托管API时,User.Identity.Name返回空,并且IIS要求提供凭据

时间:2019-05-01 06:06:09

标签: c# iis asp.net-web-api windows-authentication

托管在IIS上的

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>

0 个答案:

没有答案