.NET MVC5的身份认证User.Identity.Name获取值总是为空解决方法

 首先需要配置Web.config文件,配置内容如下:

<system.web>  

  <!--指定认证方式为Forms-->

  <authentication mode="Forms">

    <!--loginUrl的值为你登陆页面url-->

    <forms loginUrl="~/User/Login"  slidingExpiration="true"></forms>
  </authentication>

</system.web>

配置好Web.config后在你的用户登录验证通过的位置加上一句代码:

//sUserName之为你的用户名

FormsAuthentication.SetAuthCookie(sUserName, false);

快去试试吧!

猜你喜欢

转载自www.cnblogs.com/lyzi/p/10681319.html