ASP.NET2.0應用中定製安全憑證之實踐篇

來源:互聯網
上載者:User
asp.net|安全     一、方案架構

  本方案架構很簡單——它用一個Web服務來封裝ASP.NET 2.0提供者並且為遠程客戶暴露該憑證管理,你甚至還能在該架構中加上一些失去的功能。然後,在提供一個豐富的使用者介面和全面憑證管理經驗的同時,使用一個Windows表單應用程式來消費該Web服務。該Web服務組態檔將包含特定於該憑證儲存的指令。然而,這的確意味著所有由該Web服務管理的應用程式都將可以共用這些指令。

  儘管你能夠從頭到尾地構建該Web服務,也就是說,首先用靜態方法Roles和Membership來封裝它們並定義該Web服務,我卻更喜歡一種契約驅動的方法:首先設計執行各種操作的最好介面將是什麼,並且直到需要時才考慮怎樣實現它們。這樣做可以確保由Web服務暴露的介面支援所有要求的管理功能並且還將減少該客戶應用程式與任何實現細節(例如封裝提供者)之間的耦合。

  ASP.NET 2.0的一個更好的特點是它支援Web服務介面,你可以定義並且讓該Web服務暴露邏輯介面,就象類的表現一樣。為此,你需要用WebServiceBinding屬性修飾你的介面並且經由WebMethod屬性來暴露單個的介面方法。然後,你將有一個派生於這個介面的類並實現該介面,而且編譯器將要求你支援該介面的所有方法。

  為了管理和互動於憑證儲存和Web服務配置,我定義了5個介面-IApplicationManager,IMembershipManager,IPasswordManager,IroleManager和IUserManager。

  (一) IApplicationManager

  該IApplicationManager介面顯示於所附源碼中的列表2,允許管理員刪除一指定的應用程式-也就是說,從資料庫中刪除所有到它的參考並且刪除它的所有使用者和角色。IApplicationManager允許從儲存中刪除所有的應用程式,並且它能返回在該儲存中的所有應用程式的一個列表。注意,這個介面作為一個內部的介面被定義-public或internal可見度修飾詞對Web服務介面都是無意義的。該介面上的每個方法用WebMethod屬性加以修飾並有一個該方法的簡短描述。此外,存取憑證儲存的所有方法都被設定為使用交易處理。這樣以來,兩種操作-如刪除一應用程式和建立一使用者將在彼此完全隔離的情況下執行,從而保證了如刪除所有使用者等複雜操作的原子性。.NET 2.0中的Web服務只能啟動一個新事務,而且它是由WebMethod屬性的TransactionOption屬性來控制的。最後一點是把WebServiceBinding屬性應用於介面上。這就指定該介面是一個客戶和服務都能綁定到的Web服務介面。為了把該介面以一個WSDL契約方式暴露給外界,你需要使用一個shim類。這個shim類的設計是必要的,因為你不能把一個介面作為一Web服務暴露,而且你也不能在其上應用WebService屬性。這個shim類還將經由WebService屬性為該介面命名空間定義。下面的代碼顯示了IApplicationManagerShim抽象類別的定義。

[WebService(Name="IApplicationManager",
Namespace="http://CredentialsServices",
Description="IApplicationManager is used to manage
applications. This web service is only
the definition of the interface. You
cannot invoke method calls on it.")]
abstract class IApplicationManagerShim : IApplicationManager{
 public abstract void DeleteApplication(string application);
 public abstract string[] GetApplications();
 public abstract void DeleteAllApplications();
}


  因為IApplicationManagerShim是一個類,所以你可以把它暴露為一個Web服務。因為它是一抽象類別且所有方法被定義為抽象方法,所以不需要(也不能)實現任何方法。為了使其看起來就象該介面,IapplicationManagerShim把WebService屬性的屬性名稱設定為IApplicationManager(代替預設的類名)。現在,你可以使用IApplicationManager.asmx檔案來暴露該介面。

<%@ WebService Language="C#"
CodeBehind="~/App_Code/IApplicationManagerShim.cs"
Class="IApplicationManagerShim"%>


  現在,如果你瀏覽到IApplicationManager.asmx頁面,你就會看到該介面定義。你可以使用WSDL.exe的serverInterface選項來把介面定義輸入到用戶端或任何其它想綁定到該介面定義上的服務。

  (二) IMembershipManager

  IMembershipManager介面(見所附源碼中的列表3)允許你系統管理使用者帳戶的所有方面-建立和刪除使用者帳戶,更新使用者帳戶,檢索使用者帳戶細節以及檢索在一應用程式中的所有使用者。

  (三) IRoleManager

  IRoleManager介面允許你管理邏輯角色的所有方面-建立和刪除角色,從角色中增加和刪除使用者以及檢索在一應用程式中的所有角色。

[WebServiceBinding("IRoleManager")]
interface IRoleManager{
[WebMethod(...)]
void CreateRole(string application,string role);
[WebMethod(...)]
bool DeleteRole(string application,string role,bool throwOnPopulatedRole);
[WebMethod(...)]
void AddUserToRole(string application,string userName, string role);
[WebMethod(...)]
void DeleteAllRoles(string application,bool throwOnPopulatedRole);
[WebMethod(...)]
string[] GetAllRoles(string application);
[WebMethod(...)]
string[] GetRolesForUser(string application,string userName);
[WebMethod(...)]
string[] GetUsersInRole(string application, string role);
[WebMethod(...)]
void RemoveUserFromRole(string application,string userName, string roleName);
//更多成員
}

  (四) IPasswordManager

  這個IPasswordManager介面主要提供與應用程式口令策略相關的唯讀資訊。

[WebServiceBinding("IPasswordManager")]
interface IPasswordManager{
[WebMethod(...)]
bool EnablePasswordReset(string application);
[WebMethod(...)]
bool EnablePasswordRetrieval(string application);
[WebMethod(...)]
string GeneratePassword(string application,int length,
int numberOfNonAlphanumericCharacters);
[WebMethod(...)]
bool RequiresQuestionAndAnswer(string application);
[WebMethod(...)]
string ResetPassword(string application,string userName);
[WebMethod(...)]
string GetPassword(string application,string userName,string passwordAnswer);
[WebMethod(...)]
void ChangePassword(string application,string userName,string newPassword);
//更多成員
}


  典型地,該策略儲存在應用程式的設定檔中。該策略包括是否啟動口令重設和檢索,口令強度和口令回答策略等。你也可以使用IpasswordManager來產生一相應於該口令強度策略的新口令。另外,IpasswordManager可用於重設、改變或檢索一指定使用者的口令。

  (五) IUserManager

  IUserManager介面允許校正使用者憑證,檢索角色身份以及擷取指定使用者是其成員之一的所有角色。該介面用於測試和分析目的。

[WebServiceBinding("IUserManager")]
public interface IUserManager{
[WebMethod(...)]
bool Authenticate(string applicationName,string userName, string password);
[WebMethod(...)]
bool IsInRole(string applicationName,string userName, string role);
[WebMethod(...)]
string[] GetRoles(string applicationName,string userName);
}

[1] [2] [3] 下一頁  



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.