2013-10-25 36 views
2

我跑CustomAuthenticationMvc的ServiceStack用例的例子,但磨片,我尝试登录我在asp MVC登录PUR页面ServiceStack CustomAuthenticationMvc管理员密码?

用户:admin密码 :123

但显示错误msg(无效的用户名或密码),所以在CustomValidation的项目中,但在ASP.NET中,它们是用于访问HelloService的用户名和密码...因此,CustomAuthenticationMvc中用于访问HelloService的用户名和密码是什么?

感谢

回答

2

如果你看一下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"); 

在种子方法的地方。为了简单起见,您可以将其放入AccountControllerLogin方法中。


或者,如果你不能被所有的困扰,你可以只是模仿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"; 
} 
+0

谢谢!它只是正常工作!真的很有帮助@ngm –