【发布时间】:2020-03-04 19:27:01
【问题描述】:
我已经使用 Angular x.x 构建了一个前端,它可以获取现有 ASP.NET Web API 1.x-Application 的路由。
身份验证以及与后端的通信存在一些问题,因此我在客户端实现了以下拦截器:
凭证的 Angular-Interceptor:
@Injectable()
export class CredentialsInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
req = req.clone({
withCredentials: true
});
return next.handle(req);
}
}
为了处理现有的 CORS 冲突,我修改了 Global.asax,如下所示:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string httpOrigin = HttpContext.Current.Request.Params["HTTP_ORIGIN"] ?? HttpContext.Current.Request.Params["ORIGIN"] ?? "*";
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", httpOrigin);
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Token, withCredentials");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.StatusCode = 200;
var httpApplication = sender as HttpApplication;
httpApplication.CompleteRequest();
}
}
还有这样的 web.config:
<system.web>
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="*"/>
<deny users="?"/>
</authorization>
</system.web>
<system.webServer>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
在我的本地计算机上,可以通过 localhost:80(使用 IIS Express)访问后端,而我通过 IIS 10 在 192.168.xxx.xx:80 托管了我的 Angular 应用程序。
在这种环境下,一切正常,我没有任何问题。
测试环境是一个装有 Windows Server 2008 R2 和 IIS 7.5 的服务器。对于 Deep-Link-Usage URL-Rewriter 2 已安装
在构建后端和前端并部署到上述环境之后,除了一个大缺陷之外,应用程序按预期工作。
IIS 中的后端地址为 *:80、*:8080 等。
IIS 中的 Frontend-Address 是 Server-IP:80
虽然一些路由按预期工作,但在本地设置中工作的其他一些路由正在产生以下 CORS 问题:
Access to XMLHttpRequest at *route* from origin *origin* has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No "Access-Control-Allow-Origin" header is present on the requested source.
到目前为止,我找不到工作的控制器和不工作的控制器之间有什么区别。
有人知道这里发生了什么吗?
【问题讨论】:
标签: angular iis asp.net-web-api cors windows-authentication