User.Identity.Name
为空。当IP地址放在URL中而不是localhost时,IIS也会要求提供凭据。请提出解决方法。
在IIS中启用了Windows和匿名身份验证。
我尝试在IIS中启用和禁用匿名身份验证
namespace System.Web.Mvc
{
public class AccessController : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if(!isAuthorized)
{
return false;
}
return base.AuthorizeCore(httpContext);
}
}
}
我的控制器
[System.Web.Http.HttpGet]
[AccessController]
[System.Web.Http.Route("api/GetUser")]
public string GetCurrenTUser()
{
if(User.Identity.IsAuthenticated)
{
return User.Identity.Name.Replace("EMEA\\", "");
}
return "false";
}
Web.Config
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<authentication mode="Windows"/>
</system.web>