如果你看一下AppHost.cs,你可以看到如何执行验证:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (!Membership.ValidateUser(userName, password)) return false;
....
}
}
所以它使用的是成员资格提供框架,以验证用户身份。
默认情况下,它会使用SimpleMembership(见例如http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx),默认连接从web.config来:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-CustomAuthenticationMvc-20121011213234;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
你会注意到DB(aspnet-CustomAuthenticationMvc-20121011213234
) - 它实际上并不存在。您必须在Visual Studio中通过Server Explorer
创建它。
然后你必须在数据库中创建一个用户。一个快速的方法是把以下内容:
if (!WebSecurity.UserExists("testuser"))
WebSecurity.CreateUserAndAccount("testuser", "secret");
在种子方法的地方。为了简单起见,您可以将其放入AccountController
的Login
方法中。
或者,如果你不能被所有的困扰,你可以只是模仿CustomAuthentication项目中的代码。
即更换
if (!Membership.ValidateUser(userName, password)) return false;
与
if (!CheckInDB(userName, password)) return false;
...
private bool CheckInDB(string userName, string password)
{
if (userName != "admin" && userName != "user") return false;
return password == "123";
}
来源
2013-10-26 03:39:12
ngm
谢谢!它只是正常工作!真的很有帮助@ngm –