.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);

快去试试吧!

posted @ 2019-04-10 09:28  老柚子  阅读(1930)  评论(0编辑  收藏  举报