加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-5 – MVC 5表单身份验证为User.Identity.Name返回nu

发布时间:2020-12-16 06:38:44 所属栏目:asp.Net 来源:网络整理
导读:身份验证无法在我的MVC 5应用程序中进行表单身份验证身份验证.页面被正确重定向,但User.Identity.IsAuthenticated和User.Identity.Name值为空. 我的webconfig, system.web authentication mode="Forms" forms cookieless="UseCookies" defaultUrl="~/" login
身份验证无法在我的MVC 5应用程序中进行表单身份验证身份验证.页面被正确重定向,但User.Identity.IsAuthenticated和User.Identity.Name值为空.

我的webconfig,

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

UserController的,

[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 authenticnticaiton相关的配置文件(startup.auth.cs).我删除了它们并添加了上面的appsetting条目,因为它需要停止加载Owin程序集.

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

解决方法

如果您的项目默认情况下具有Owin身份验证,则它将从项目中删除表单身份验证.
如果您看到您的网络配置,您可能会看到

<remove name="FormsAuthentication" />

组态.

只需删除它.

我有同样的问题,它解决了这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读