2011-08-05 32 views
3

我正在尋找一個關於爲ASP.Net MVC3創建登錄系統而不使用默認ASP.Net成員資格提供程序的教程或小介紹性指南。關於創建沒有默認ASP.Net成員資格提供者的MVC3登錄系統的任何教程?

我已經創建了互聯網應用模板的新項目MVC3,這裏是_LogOnPartial.cshtml的內容:

@if(Request.IsAuthenticated) { 
    <text>Welcome <strong>@User.Identity.Name</strong>! 
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text> 
} 
else { 
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ] 
} 

是成員資格提供@User.Identity.Name一部分?我想閱讀更多關於ASP.Net MVC3的會員資格,因爲我已經擁有了一個擁有用戶證書的數據庫,我不需要使用這個數據庫。

謝謝!

+1

我沒有特定教程的建議,但我可以說的是,用戶對象不是部分成員資格提供者具體...如果您區分成員資格提供者和FormsAuthentication提供者。在我最新的項目中,我編寫了自己的用戶數據存儲和密碼哈希/驗證,但仍然使用FormsAuthentication提供程序來跟蹤用戶的會話。 –

回答

4

在ASP.NET MVC中實現自定義身份驗證代碼其實很簡單。

在您的控制器的LogOn方法中,您需要在驗證用戶憑證後調用FormsAuthentication提供程序。

public ActionResult LogOn(LogOnModel model) 
{ 
    //Handle custom authorization then call the FormsAuthentication provider 

    FormsAuthentication.SetAuthCookie(/*user name*/, true); 

    //Return view 
} 

調用此方法後,User.Identity.Name將被填充,你可以利用你的控制器或控制方法AuthorizeAttribute

在控制器的LogOff方法,你會再次調用FormsAuthentication提供商

public ActionResult LogOff() 
{ 
    FormsAuthentication.SignOut(); 

    //Return view 
} 
+0

我找不到SignIn方法。你確定它在嗎? –

+0

@Sergio對不起,我在意外地使用了我在包裝類上的名字。它的FormsAuthentication.SetAuthCookie –

相關問題