C# User.Identity.Name为空
我对此做了很多研究,但找不到解决这个问题的方法 my web.config:C# User.Identity.Name为空,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我对此做了很多研究,但找不到解决这个问题的方法 my web.config: <system.web> <compilation debug="true" targetFramework="4.5.1"/> <httpRuntime targetFramework="4.5.2"/> <authentication mode="Windows"/> <authorization> <
<system.web>
<compilation debug="true" targetFramework="4.5.1"/>
<httpRuntime targetFramework="4.5.2"/>
<authentication mode="Windows"/>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
总是空白的
IIS Windows身份验证是唯一启用的选项
我已经试过了
[Authorize]
同样的结果
我检查了IIS配置文件以确保
<windowsAuthentication enabled="true">
设置为true。您是否在授权的上下文中工作?换句话说,除非你真的说你想授权某个特定的控制器或操作,否则即使用户以前登录过,也不会填充任何内容。例如:
public ActionResult Foo()
{
var user = User.Identity.Name; // null
}
但是
如果需要该操作仍允许匿名访问,只需添加[AllowAnonymous]
:
[Authorize]
[AllowAnonymous]
public ActionResult Foo()
{
...
你看过吗
[Authorize]
public ActionResult Foo()
{
var user = User.Identity.Name; // WIN
}
[Authorize]
[AllowAnonymous]
public ActionResult Foo()
{
...