gpt4 book ai didi

asp.net-mvc - ASP.NET MVC HttpContext 在登录后获取 IdentityName

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

HttpContext.Current.User.Identity.Name 在登录后返回 null。我使用的是 IIS7.0 框架 4.0。和 vs 2010。我有另一个项目,targetFramework 是 3.5。它运作良好。但是我的新项目的targetFramework是4.0。并且在调用 HttpContext.Current.User.Identity.Name 时返回 null

最佳答案

在能够使用此属性之前,您应该在登录后发出 HTTP 重定向。重定向后,您将能够在后续请求中使用它。这是通常的模式:

public ActionResult LogOn()
{
FormsAuthentication.SetAuthCookie("someuser", false);
return RedirectToAction("foo");
}

[Authorize]
public ActionResult Foo()
{
// use the logged in user here without problems
string userName = User.Identity.Name;
return View();
}

关于asp.net-mvc - ASP.NET MVC HttpContext 在登录后获取 IdentityName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291365/

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