gpt4 book ai didi

asp.net-mvc-5 - MVC 5 Forms 身份验证为 User.Identity.Name 返回 null

转载 作者:行者123 更新时间:2023-12-02 05:01:25 25 4
gpt4 key购买 nike

身份验证无法在我的 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>

用户 Controller ,

 [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"/>

最佳答案

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

<remove name="FormsAuthentication" />

配置。

只需将其删除即可。

我也遇到了同样的问题,解决了。

关于asp.net-mvc-5 - MVC 5 Forms 身份验证为 User.Identity.Name 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com