2013-10-25 127 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 –