2016-07-07 58 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" /> - >

,并开始工作。

相关问题