ASP.Net 2中的登录组件
-
实现自己的Membership Provider(继承自MembershipProvider类)
- MembershipProvider类(详细内容见MSDN中关于MembershipProvider类的说明)
受保护的构造函数
公共属性
公共方法 (请参见 受保护的方法 )
受保护的方法
公共事件
|
- 实现自己的SimpleMembershipProvider类
/// <summary> /// SimpleMembershipProvider 的摘要说明 /// </summary> public class SimpleMembershipProvider : MembershipProvider { public SimpleMembershipProvider() { // // TODO: 在此处添加构造函数逻辑 // }
public override string ApplicationName { get { return ("SimpleMembershipProvider Application"); } set { } }
public override bool ChangePassword(string username, string oldPassword, string newPassword) { return (true); //throw new Exception("The method or operation is not implemented."); }
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) { return (true); //throw new Exception("The method or operation is not implemented."); }
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { status = MembershipCreateStatus.Success; return (null); //throw new Exception("The method or operation is not implemented."); }
public override bool DeleteUser(string username, bool deleteAllRelatedData) { return (true); //throw new Exception("The method or operation is not implemented."); }
public override bool EnablePasswordReset { get { return (true); } }
public override bool EnablePasswordRetrieval { get { return (true); } }
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) { totalRecords = 0; return (null); //throw new Exception("The method or operation is not implemented."); }
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) { totalRecords = 0; return (null); //throw new Exception("The method or operation is not implemented."); }
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { totalRecords = 0; return (null); //throw new Exception("The method or operation is not implemented."); }
public override int GetNumberOfUsersOnline() { return (1); //throw new Exception("The method or operation is not implemented."); }
public override string GetPassword(string username, string answer) { return (string.Empty); //throw new Exception("The method or operation is not implemented."); }
public override MembershipUser GetUser(string username, bool userIsOnline) { return (null); //throw new Exception("The method or operation is not implemented."); }
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { return (null); //throw new Exception("The method or operation is not implemented."); }
public override string GetUserNameByEmail(string email) { return (string.Empty); //throw new Exception("The method or operation is not implemented."); }
public override int MaxInvalidPasswordAttempts { get { return (7); } }
public override int MinRequiredNonAlphanumericCharacters { get { return (1); } }
public override int MinRequiredPasswordLength { get { return (1); } }
public override int PasswordAttemptWindow { get { return (1); } }
public override MembershipPasswordFormat PasswordFormat { get { return (MembershipPasswordFormat.Clear); } }
public override string PasswordStrengthRegularExpression { get { return (string.Empty); } }
public override bool RequiresQuestionAndAnswer { get { return (true); } }
public override bool RequiresUniqueEmail { get { return (true); } }
public override string ResetPassword(string username, string answer) { return (string.Empty); //throw new Exception("The method or operation is not implemented."); }
public override bool UnlockUser(string userName) { return (false); //throw new Exception("The method or operation is not implemented."); }
public override void UpdateUser(MembershipUser user) { //throw new Exception("The method or operation is not implemented."); }
public override bool ValidateUser(string username, string password) { if (username.ToLower() == "gavinlv") { if (password == "GavinLv") { return (true); } } return (false); } } |
这里仅仅为了实验用,所以仅简单的实现了ValidateUser(string, string) : bool方法,这个方法用来验证用户名和密码是否正确,并返回验证结果(True|False)。
-
创建一个新的Web项目
- 在Default.aspx上放一个LoginStatus控件(System.Web.UI.WebControls.LoginStatus),并命名为loginStatus。这个控件将根据当前账号登录的状态的不同而自动显示不同的状态,账号已登录后为"注销",账号登录前为"登录"。
- 在Default.aspx上放一个LoginName控件(System.Web.UI.WebControls.LoginName),并命名为loginName。这个控件将显示当前登录的账号名。
- 新建一个Web窗体:Login.aspx
- 在Login.aspx上放一个Login控件(System.Web.UI.WebControls.Login),并命名为login
- 在属性窗口中设置login控件的DestinationPageUrl设置为Default.aspx
-
配置Web.config文件
- 如果项目中没有web.config文件,那就添加一