在我的 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/