简体   繁体   English

User.Identity.Name为空白

[英]User.Identity.Name is blank

I have done a bunch of research about this, but can't find a solution to this problem. 我已经对此进行了大量研究,但是找不到解决该问题的方法。

my web.config : 我的web.config:

  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.2"/>
    <authentication mode="Windows"/>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

and for some reason 由于某种原因

string User = User.Identity.Name;

is always blank. 始终为空。

IIS Windows authentication is the only option that is enabled. IIS Windows身份验证是唯一启用的选项。

i have tried the 我试过了

[Authorize]

same result. 同样的结果。

I checked the IIS config file to make sure 我检查了IIS配置文件以确保

  <windowsAuthentication enabled="true">

is set to true. 设置为true。

Are you working within an authorized context? 您是否在授权的环境中工作? In other words, unless you actually say you want a particular controller or action authorized, nothing is filled even if the user has previously logged in. For example: 换句话说,除非您实际上说您要授权特定的控制器或操作,否则即使用户先前已登录也不会填充任何内容。例如:

public ActionResult Foo()
{
    var user = User.Identity.Name; // null
}

But

[Authorize]
public ActionResult Foo()
{
   var user = User.Identity.Name; // WIN
}

If you need the action to still allow anonymous access, just add [AllowAnonymous] as well: 如果您需要采取措施仍允许匿名访问,则只需添加[AllowAnonymous]

[Authorize]
[AllowAnonymous]
public ActionResult Foo()
{
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM