【问题标题】:MVC 5 Forms authentication retuns null for User.Identity.NameMVC 5 Forms 身份验证为 User.Identity.Name 返回 null
【发布时间】:2015-01-06 12:47:39
【问题描述】:

身份验证无法在我的 MVC 5 应用程序中对表单身份验证进行身份验证。页面被正确重定向,但 User.Identity.IsAuthenticated 和 User.Identity.Name 值为空。

我的网络配置,

  <system.web>        
    <authentication mode="Forms">
          <forms cookieless="UseCookies" defaultUrl="~/" loginUrl="~/user/signin"  name="MYAPPWeb" timeout="21600" slidingExpiration="true"/>
        </authentication>

用户控制器,

 [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SignIn(SignInViewModel user)
        {

                        UserDTO userObj;
                        using (var services = new ServiceFactory())
                        {                            
                            userObj = services.UserManagement.ValidateLoginDetails(ConfigHelper.EnvironmentString, user.TenantName, user.Username, user.Password);
                        }
                                string userID = userObj.UserID.ToString();
                                //FormsAuthentication.RedirectFromLoginPage(userID, user.RememberMe);
                                FormsAuthentication.SetAuthCookie(userID.ToString(),true);

                                FormsAuthentication.RedirectFromLoginPage(userID, false); //DO NOT REMEMBER ME

}

HomeController(默认页面)

 public ActionResult Index()
        {

            bool x = User.Identity.IsAuthenticated; //false?
            string y = User.Identity.Name; //null?

return View();
}

它看起来很简单,我错过了什么吗?请帮忙!

注意:
当我创建项目时,我选择了 Windows 身份验证。它创建了一些与 Owin 身份验证相关的配置 cs 文件(startup.auth.cs)。我已经删除了它们并添加了上面的 appsetting 条目,因为它需要停止加载 Owin 程序集。

<add key="owin:AutomaticAppStartup" value="false"/>

【问题讨论】:

    标签: asp.net-mvc-5 forms-authentication


    【解决方案1】:

    如果你的项目默认有 Owin 认证,它会从项目中移除表单认证。 如果您看到您的网络配置,您可能会看到

    <remove name="FormsAuthentication" />
    

    配置。

    只需删除它。

    我遇到了同样的问题,它解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2021-09-27
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多