2013-10-09 44 views
2

我正在用VS2013 RC和MVC 5構建網站,並且正在嘗試使用formsAuthentification而無需在我的網站上註冊永久用戶。(Request.IsAuthenticated)在使用FormsAuthentication.SetAuthCookie(用戶名,false)後爲false

我發佈到我公司的API來驗證用戶的名稱和密碼。當這個成功回來時,我想發出一個授權cookie:

System.Web.Security.FormsAuthentication.SetAuthCookie(username, false); 

在調用它之後,我看到.ASPXAUTH = ... cookie。

但是,我無法進入模板的_LoginPartial.cshtml頁面上的@if(User.Identity.IsAuthenticated)或者@if(Request.IsAuthenticated)塊。

這種技術在MVC 4中對我有用,我試圖將它彎曲以適合MVC 5的OWIN認證。

+0

答案是好的評論,但我覺得這個問題是無關的MVC 5,因爲我在MVC 4中遇到了同樣的問題。 –

回答

16

我需要使窗體身份驗證在web.config

<system.web> 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="2880" /> 
</authentication> 
... 
</system.web> 
0

那些誰仍然有這個問題,並嘗試我不建議嘗試添加到部門認證形式Web.Config文件中的所有上述方法無Cookie =「UseCookies」。在我的情況下,它工作得很好。

<system.web> 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" cookieless="UseCookies" timeout="2880" /> 
</authentication> 
... 
</system.web> 
0

我嘗試了所有上述解決方案,但解決我的問題的事情是在web.config中

<modules> 
<remove name="FormsAuthentication"/> 
</modules> 
相關問題