繁体   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