简体   繁体   中英

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.

<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:

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

Follows the code to the 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 '*'.

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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