阅读提要 在缺省状况下,你只能使用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] 下一页 |
温馨提示:喜欢本站的话,请收藏一下本站!