简体   繁体   English

在 asp.net mvc 中使用 signalR 时,如何启用 cors?

[英]How can I enable cors when using signalR in asp.net mvc?

I use signalR in asp.net mvc for web api and I need to enable cors in web.config. I use signalR in asp.net mvc for web api and I need to enable cors in web.config.

<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true"/>
<add name="Access-Control-Allow-Headers" value="X-Requested-With, origin, content-type, accept" />

But when I'm going to connect to signalR, it displays the following error:但是当我要连接到 signalR 时,它会显示以下错误:

has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:当请求的凭据模式为'包括

Follows the code to the startup.cs:遵循startup.cs的代码:

[EnableCors("CorsPolicy")]
public void Configuration(IAppBuilder app)
{
    app.MapSignalR();
}

How can I fix it?我该如何解决?

As your error says:正如你的错误所说:

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'.响应中的“Access-Control-Allow-Origin”header 的值不能是通配符“*”。

And you have:你有:

<add name="Access-Control-Allow-Origin" value="*" />

So in this case you should provide the origin from where you are connecting.因此,在这种情况下,您应该提供连接的来源。 If you are trying to connect from the front-end you can use this as an example:如果您尝试从前端连接,则可以使用以下示例:

<add name="Access-Control-Allow-Origin" value="https://localhost:4200"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM