2016-07-07 162 views
0

我有一個簡單的ASP.NET MVC應用程序,我使用窗體身份驗證。當單擊Login按鈕時,我設置了表單auth cookie。User.Identity.Name空

FormsAuthentication.SetAuthCookie("userid",false); 

的問題是,在隨後的請求有Sy​​stem.Web.HttpContext.Current.User.Identity總是顯示IsAuthenticated = false,並且名稱爲空。由於這一點,[授權]屬性總是失敗。但是,我可以看到後面的請求在標頭中有cookie .ASPXAUTH。我的Web.Config文件有<authentication mode="Forms" />

可能是什麼問題?

+0

您是否使用默認的MVC模板創建項目? – DavidG

+0

@DavidG是的。我使用默認模板 – Thomas

+0

不應該使用FormsAuthentication.SetAuthCookie(model.UserName,false); – iamrico

回答

0

在你的啓動代碼,你有類似於這樣:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/account/login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       // Enables the application to validate the security stamp when the user logs in. 
       // This is a security feature which is used when you change a password or add an external login to your account. 
       OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User, int>(
        validateInterval: TimeSpan.FromMinutes(30), 
        regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), 
        getUserIdCallback: (identity) => identity.GetUserId<int>() 
       ) 
      } 
     }); 

很肯定這是解決辦法,當我有類似的問題,但它以前是幾年,所以我不記得了100%。

0

我注意到我在我的Web.config中有以下內容。

<modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 

我評論的:

<!--<remove name="FormsAuthentication" /> - >

,並開始工作。