C# HttpContext.User.Identity.Name 返回空白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13939128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:08:54  来源:igfitidea点击:

HttpContext.User.Identity.Name is returning blank

c#asp.net-mvc

提问by xivo

I am trying to figure out why HttpContext.User.Identity.Nameis returning blank.

我想弄清楚为什么HttpContext.User.Identity.Name返回空白。

Code

代码

public ActionResult Test()
{
    string username = HttpContext.User.Identity.Name;
    return Content(username);         
}

Am I using this in the wrong context? I am trying to get the user's username.

我在错误的上下文中使用它吗?我正在尝试获取用户的用户名。

Web.Config

网页配置

<authentication mode="Windows" />

IIS

信息系统

I have enabled Anonymous and nothing else is checked. I am running IIS 6.0.

我启用了匿名,没有检查其他任何内容。我正在运行 IIS 6.0。

Is there any type of information I need to add to assist with figuring this out? I am pretty stuck. I checked this questionbut do I need to set a Cookie to make this work?

我需要添加任何类型的信息来帮助解决这个问题吗?我很困。我检查了这个问题,但是我需要设置一个 Cookie 才能完成这项工作吗?

回答by jrummell

I have enabled Anonymous and nothing else is checked. I am running IIS 6.0.

我启用了匿名,没有检查其他任何内容。我正在运行 IIS 6.0。

This means that you won't be prompted to login, so User.Identity.IsAuthenticatedwill be false and User.Identity.Namewill be blank.

这意味着不会提示您登录,因此User.Identity.IsAuthenticated将是 false 并且User.Identity.Name为空白。

Uncheck Anonymous Authentication and check Windows Authentication.

取消选中匿名身份验证并选中 Windows 身份验证。

回答by xdumaine

IsAuthenticated returns false, and thus Identity.Name returns empty string because you haven't required authentication for that action. You have to enable Windows Authentication and require authentication for the action. Try requiring that the user be authorized for the action by decorating it with the [Authorize]attribute - which will initiate the authentication negotiation.

IsAuthenticated 返回 false,因此 Identity.Name 返回空字符串,因为您不需要对该操作进行身份验证。您必须启用 Windows 身份验证并要求对操作进行身份验证。尝试通过使用[Authorize]属性来装饰它来要求用户被授权执行该操作- 这将启动身份验证协商。

[Authorize]
public ActionResult Test()
{
    if(Request.IsAuthenticated)
    {
        string username = HttpContext.User.Identity.Name;
        return Content(username);         
    }
    else 
    {
        return Content("User is not authenticated");
    }
}

回答by Dan

If anyone else is experiencing this issue, verify "Load User Profile" option is set to true. Go to IIS application pools. Select your app pool from the list. Click on Advanced Settings and scroll down to Process Model section.

如果其他人遇到此问题,请确认“加载用户配置文件”选项设置为true。转到 IIS 应用程序池。从列表中选择您的应用程序池。单击高级设置并向下滚动到流程模型部分。

This solved the problem in my case.

这解决了我的问题。

回答by Artur Alexeev

If you are using FormsAuthentication.SetAuthCookie then you need to add

如果你使用 FormsAuthentication.SetAuthCookie 那么你需要添加

<authentication mode="Forms" />

to

<System.Web>

in Web.config file. Solution from here

在 Web.config 文件中。从这里解决