您的位置:首页 > 编程语言 > ASP

ASP.Net 2 入门(二)――用户登录和注销

2012-11-13 22:22 423 查看
实现自己的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, stringnewPassword)

{

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, intpageIndex, 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, intpageIndex, 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 inttotalRecords)

{

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文件,那就添加一个

设置authentication字段

<authentication mode="Forms">

<forms loginUrl="login.aspx"

name=".ASPXFORMSAUTH" />

</authentication>

设置membership字段,使前面的SimpleMembershipProvider类实现其价值

<membership defaultProvider="SimpleProvider" userIsOnlineTimeWindow="15">

<providers>

<add

name="SimpleProvider"

type="SimpleMembershipProvider"

enablePasswordRetrieval="true"

enablePasswordReset="true"

requiresQuestionAndAnswer="true"

writeExceptionsToEventLog="true" />

</providers>

</membership>

完成

编译、查看网页,登录注销功能就搞定了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐