杭州慈溪职高摸奶门,郭德纲国庆专场,3u8844
将 signalr 集成到 asp.net core mvc 程序的时候,按照官方 demo 配置完成,但使用 demo 页面建立连接一直提示如下信息。
access to xmlhttprequest at 'http://localhost:8090/signalr-mychathub/negotiate' from origin 'null' 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'. the credentials mode of requests initiated by the xmlhttprequest is controlled by the withcredentials attribute.
原始代码:
services.addcors(op => { op.addpolicy(monitorstartupconsts.defaultcorspolicyname, set => { set.allowanyorigin() .allowanyheader() .allowanymethod() .allowcredentials(); }); });
出现该问题的原因是由于 cors 策略设置不正确造成的,原始设置我是允许所有 origin 来源。但是由于 dotnetcore 2.2 的限制,无法使用 allowanyorigin()
+ allowcredentials()
的组合,只能显式指定 origin 来源,或者通过下述方式来间接实现。
更改 cors 相关配置,在 corspolicybuilder
提供了一个方法用于配置验证逻辑。该方法名字叫做 setisoriginallowed(func<string, bool> isoriginallowed)
,这个委托会验证传入的 origin 源,如果验证通过则返回 true
。
在这里我们只需要将其设置为一直返回 true
即可。
最终代码如下:
services.addcors(op => { op.addpolicy(monitorstartupconsts.defaultcorspolicyname, set => { set.setisoriginallowed(origin => true) .allowanyheader() .allowanymethod() .allowcredentials(); }); });
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论