閱讀提要 在缺省狀況下,你只能使用Visual Studio 2005的一個本機實例來管理與ASP.NET 2.0一同發行的SQL Server數據庫中的安全憑證。本文將向你展示怎樣用一個Web服務來包裝ASP.NET 2.0提供者并通過使用一個Windows表單應用程序來管理憑證存儲從而擴展這種管理能力。 Membership Provider負責管理用戶,而Role Provider負責管理角色。在憑證存儲中,每個用戶或角色僅限于一應用程序之內。這樣就允許不同應用程序使用一樣的憑證存儲而不會與彼此的用戶名或角色相沖突。ASP.NET為SQL服務器、Windows和活動目錄(見圖1)等的憑證存儲提供支持。為了安裝SQL Server憑證數據庫,可以運行aspnet_regsql.exe程序,其位置是: <WINDOWS>\Microsoft.NET\Framework\<version> 這個安裝程序創建一個稱為aspnetdb的新數據庫-它包含一組應用程序的表、用戶、角色以及存取這些表的存儲過程。這個SQL Server數據庫是運用最新的安全技術經過精心設計的。另外,ASP.NET 2.0還提供一套相應于提供者的類(圖1)。 使用哪個提供者的信息被保存在應用程序的配置文件(App.Config或Web.Config)中。你幾乎不需要直接與特定的提供者進行交互;而是,存在兩個靜態助理類:Membership和Roles-它們負責從配置文件中讀取使用哪個提供者。默認的提供者(即當沒有指定提供者時)就是SQL Server。Membership類(列表1)允許你創建和刪除用戶,檢索關于用戶的信息并觀看口令策略。 列表1: Membership助理類 [Serializable] public class MembershipUser{ public virtual bool ChangePassword(string oldPassword,string newPassword); public virtual string GetPassword(string passwordAnswer); public virtual string ResetPassword(string passwordAnswer); public virtual bool UnlockUser(); //其它成員 } public static class Membership{ public static string ApplicationName{get;set;} public static MembershipUser CreateUser(string username, string password); public static MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status); public static bool DeleteUser(string username,bool deleteAllRelatedData); public static MembershipUser GetUser(string username); public static void UpdateUser(MembershipUser user); public static bool ValidateUser(string username,string password); public static bool EnablePasswordReset{get;} public static bool EnablePasswordRetrieval{get;} //其它成員 } 例如,為了在"MyApp"應用程序中創建一新用戶,你僅需如下編碼: Membership.ApplicationName = "MyApp"; Membership.CreateUser("MyUser","MyPassword",...); Roles類允許你創建和刪除用戶角色,從角色中添加或刪除用戶,檢索用戶的角色會員信息以及驗證角色會員。下面是該類的定義: public static class Roles{ public static string ApplicationName{get;set;} public static void CreateRole(string roleName); public static bool DeleteRole(string roleName, bool throwOnPopulatedRole); public static void AddUserToRole(string username, string roleName); public static void RemoveUserFromRole(string username, string roleName); public static string[] GetAllRoles(); public static string[] GetRolesForUser(string username); public static string[] GetUsersInRole(string roleName); public static bool IsUserInRole(string username, string roleName); //其它成員 } 例如,要把角色"Manager"添加到應用程序"MyApp"上,你可以如下編碼: Roles.ApplicationName = "MyApp"; Roles.CreateRole("Manager"); [1] [2] 下一頁 |
溫馨提示:喜歡本站的話,請收藏一下本站!