使用ASP.NET Membership的方法

最简单的过程如下:

1.建立一个名为 CustomMemberShipProvider类,从MembershipProvider继承;并重写其中的ValidateUser方法:

public class CustomMemberShipProvider : MembershipProvider
{
/// <summary>
/// 验证用户
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public override bool ValidateUser(string username, string password)
{
//查询用户
return 1 == new xxxEntities().Table_User
.Count(x => x.UserId == username && x.Password == password);
}


public override string ApplicationName
{
get
{
return "xxx";
}
set
{
throw new NotImplementedException();
}
}

//其余代码不做任何修改
}

2.修改web.config文件,添加一个MembershipProvide,设置如下:

    <membership defaultProvider="CustomMembershipProvide">
<providers >
<clear />
<!--<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />-->
<add name="CustomMembershipProvide" type="xxx.Models.CustomMemberShipProvider" />
</providers>
</membership>
<profile>

3.如果在Asp.net MVC中,在Action前面加上 [Authorize],即可实现匿名的身份认证。

你可能感兴趣的:(asp.net)