angular - Windows 身份验证在 Microsoft Edge 浏览器中不适用于 Angular 2 应用程序

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

在我的 Angular 2 项目中,客户端调用 Web API 方法,该方法要求用户使用 Windows 身份验证获得授权。此调用在 Internet Explorer 11、Firefox 和 Chrome 中运行良好,但在 Microsoft Edge 中运行不正常,Microsoft Edge 不显示登录对话框,在控制台中显示“状态响应:401 Unauthorized for URL”。如果我尝试直接在 Edge 中打开 API Url,它会正确显示对话框,所以如果我理解正确,问题出在 Angular 2 调用中。

有人在使用 Microsoft Edge 浏览器时遇到过同样的问题吗?是否需要一些特殊的 header 或 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>

WebApi配置:

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

全局.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();
    }
}

最佳答案

我知道这个问题很老了,但从来没有人回答过,所以这里是答案。

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

它不适用于 localhost 或 machinename。

问题请看这里

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

关于angular - Windows 身份验证在 Microsoft Edge 浏览器中不适用于 Angular 2 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41705189/

相关文章:

angular - 在模板内的 Angular 2 中引用对象数组中的对象

angular - 在 Angular cdk 叠加层上获取事件位置?

c# - WebAPI 和 ILMerge(又名自托管单个 exe)

rest - 将 RPC 样式 Web 服务操作转换为 REST 服务

sql-server - 是否有可能获得与语言无关的 "NT AUTHORITY\NETWORK SERVICE"用户?

angular - 如何更改(升级或降级)Angular 版本?

angular - 如何默认导航到子路由 - Angular 7(路由)

c# - 操作未在分配的超时 00 :01:00 内完成

asp.net - 经典 ASP 和 ASP.Net 身份验证

ruby-on-rails-4 - Rails 应用程序,带有 Windows 身份验证的 IIS 8.5 中的 REMOTE_USER 属性