Ben*_*Ben 7 c# asp.net azure cors asp.net-web-api
我正在尝试启用CORS但是我的AngularJS Web应用程序发送的每个请求都经常出现同样的问题:
跨源请求已阻止:同源策略禁止在https://.../ ... 读取远程资源(原因:缺少CORS标头'Access-Control-Allow-Origin').
它只发生在Firefox上!它与Chrome,Safari,IE和Edge完美配合.
环境:
我的API是托管在Azure App Services(IIS 8.0)上的.NET Web Api 2.3(.NET 4.6).
我的前端应用程序是一个AngularJS webapp,我通过https访问它,并通过https使用API.
我做了什么:我有一个Startup类,它被声明为Owin的启动入口点.
[assembly: OwinStartup(typeof(Startup))]
namespace SentinelleApi
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.Map("/signalr", map =>
{
map.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true,
EnableJavaScriptProxies = false
};
map.RunSignalR(hubConfiguration);
});
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个WebApiConfig类,声明如下:
namespace SentinelleApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// No need to declare config.EnableCors() here, the app.UseCors(...) does the job in startup section
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional}
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有触摸web.config手动添加标题(我试过,但它没有解决我的问题).无论如何,不建议在web.config中添加这些头文件并同时拥有app.UseCors().
所以,在Firefox上,我有CORS错误,但在Safari/Chrome上,没问题!
我有除了Firefox以外的Access-Control-Allow-Origins.
在Chrome上:
在Firefox上:
编辑: 嗯,我已经调查了一下,如果我将我的webapp地址从https://切换到http://,一切正常在Firefox上运行(同样,我的API可以由Angular通过https使用,没有CORS一点问题)......
我真的不明白发生了什么,但神奇的是,Access-Control-Allow-Origin出现在Firefox消耗的每条API路径上,而当它是安全连接(https)时则不然.我依赖来自Microsoft的https Azure证书,这似乎是有效的(锁定图标为绿色).
当然,我想保留https,但我不知道如何解决我的问题.
似乎与以下内容有关:Firefox CORS请求尽管标题为"Cross-Origin Request Blocked"
我已经针对Web应用程序客户端(主持AngularJS)制定了重写规则.在我的web.config中,我有:
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
Run Code Online (Sandbox Code Playgroud)
当我删除它时,它很好(我的Web应用程序可以使用HTTP访问),但是当我切换到HTTPS时,即使API服务器接受所有来源,它也会失败.
所以问题是:我应该添加一些特殊的东西来使HTTPS /证书与CORS一起使用(在Firefox下)吗?
还相关:https://stackoverflow.com/questions/33743645/iis-reverse-proxy-to-bypass-cors-issue
你认为你可能有这个问题Firefox CORS请求尽管标题提供'Cross-Origin Request Blocked'
Firefox使用chrome/IO中的不同证书存储区,您可以在工具 - >选项 - >高级下检查证书,然后您有一个按钮来查看ceritifcate存储.
| 归档时间: |
|
| 查看次数: |
3821 次 |
| 最近记录: |