对于Angular 2应用程序,Windows身份验证在Microsoft Edge浏览器中不起作用

时间:2017-01-17 19:15:38

标签: angular asp.net-web-api windows-authentication microsoft-edge

在我的Angular 2项目中,客户端调用Web API方法,该方法要求使用Windows身份验证授权用户。此调用在Internet Explorer 11,Firefox和Chrome中正常工作,但在Microsoft Edge中无法正常工作,Microsoft Edge未显示“登录”对话框,显示“响应状态:401未授权URL”#34;在控制台中。如果我尝试直接在Edge中打开API Url,它会正确显示对话框,因此如果我理解它是正确的,则问题出在Angular 2调用中。

有些人在Microsoft Edge浏览器中遇到过同样的问题吗?是否需要一些特殊的头或Web API配置才能解决问题?

Angular 2服务:

let headers = new Headers();
let options = new RequestOptions({ headers: headers, withCredentials: true });

return this._http.get(this._productUrl, options)
  .map((response: Response) => <IProduct[]> response.json())
  .do(data => console.log('All: ' +  JSON.stringify(data)))
  .catch(this.handleError);

WebApi 2项目中的Web.Config:

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <deny users="?" />
    </authorization>
 </system.web>

WebApiConfig:

var cors = new EnableCorsAttribute("http://localhost:3000", "*", "*") {SupportsCredentials = true};
config.EnableCors(cors);

的Global.asax:

protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:3000");
        Response.Headers.Add("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token");
        Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
        Response.Headers.Add("Access-Control-Allow-Credentials", "true");
        Response.Headers.Add("Access-Control-Max-Age", "1728000");
        Response.End();
    }
}

1 个答案:

答案 0 :(得分:0)

我知道这个问题很旧,但从未得到回答,所以这里是答案。

这是Microsoft Edge的“按设计”错误。

它不能在localhost或计算机名上使用。

在此处查看问题

https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/4776775/

相关问题