簡體   English   中英

User.Identity.Name為空

[英]User.Identity.Name empty

我有一個簡單的ASP.NET MVC應用程序,在其中使用了表單身份驗證。 單擊“登錄”按鈕時,我將設置表單身份驗證cookie。

FormsAuthentication.SetAuthCookie("userid",false);

問題在於后續請求的System.Web.HttpContext.Current.User.Identity始終顯示IsAuthenticated = false,並且Name為空。 因此,[Authorize]屬性始終失敗。 但是,我可以看到后續請求在標頭中具有cookie .ASPXAUTH。 我的Web.Config文件具有<authentication mode="Forms" />

可能是什么問題?

在啟動代碼中,您是否有類似以下內容:

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%了。

我注意到我的Web.config中包含以下內容。

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

我對此評論:

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

它開始工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM