asp.net core 使用SignalR跨域请求出现的坑 - a914541185的博客 - CSDN博客

来源: asp.net core 使用SignalR跨域请求出现的坑 – a914541185的博客 – CSDN博客

前段时间因为工作需要,认识到了SignalR,这个东西本身挺好用,但是在处理跨域问题上是遭遇了很大的坑。

我在本地通过localhost连接的时候毫无问题,但是当我在前端使用IP来连接我的后台,则会出现连接失败的问题。查阅了很多,询问公司内的大牛之后,找到一个正确解决方案,特记录如下:

首先,在Strartup.cs文件下处理跨域问题

public const string CorsName = “SignalRCors”;

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddSignalR();
services.AddCors(options =>
{
options.AddPolicy(CorsName,
policy => policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
services.AddMvc((opt) =>
{
opt.Filters.Add(new CorsAuthorizationFilterFactory(CorsName));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseCors(CorsName);
之后右键你的项目进入属性-调试,记下你的http端口号以及htts的端口号

然后找到applicationhost.config文件,一般路径是项目文件夹\.vs\config\applicationhost.config。找到如下代码

<sites>
<site name=”WebSite1″ id=”1″ serverAutoStart=”true”>
<application path=”/”>
<virtualDirectory path=”/” physicalPath=”%IIS_SITES_HOME%\WebSite1″ />
</application>
<bindings>
<binding protocol=”http” bindingInformation=”:8080:localhost” />
</bindings>
</site>
<site name=”signalrTotest” id=”2″>
<application path=”/” applicationPool=”Clr4IntegratedAppPool”>
<virtualDirectory path=”/” physicalPath=”C:\Users\JC.YX\Desktop\signalrTotest\signalrTotest” />
</application>
<bindings>
<binding protocol=”https” bindingInformation=”:44331:localhost” />
<binding protocol=”http” bindingInformation=”:35000:localhost” />
</bindings>
</site>
可以看到有https和http的端口设置,以及允许localhost连接。这时我们只需要在它们下面添加自己的IP地址就可以了。添加之后的代码如下

<sites>
<site name=”WebSite1″ id=”1″ serverAutoStart=”true”>
<application path=”/”>
<virtualDirectory path=”/” physicalPath=”%IIS_SITES_HOME%\WebSite1″ />
</application>
<bindings>
<binding protocol=”http” bindingInformation=”:8080:localhost” />
</bindings>
</site>
<site name=”signalrTotest” id=”2″>
<application path=”/” applicationPool=”Clr4IntegratedAppPool”>
<virtualDirectory path=”/” physicalPath=”C:\Users\JC.YX\Desktop\signalrTotest\signalrTotest” />
</application>
<bindings>
<binding protocol=”https” bindingInformation=”:44331:localhost” />
<binding protocol=”https” bindingInformation=”:44331:192.168.0.104″ />
<binding protocol=”http” bindingInformation=”:35000:localhost” />
<binding protocol=”http” bindingInformation=”:35000:192.168.0.104″ />
</bindings>
</site>
这时,将你的前端代码修改为对应IP地址加端口号,启动IIS服务,你可以尽情的和朋友在聊天室里聊天了

var connection = new signalR.HubConnectionBuilder().withUrl(“http://yourIP:35000/chatHub”).build();
注意:如果你前端写的是http协议,请在启动IIS服务之后,将默认打开的网址修改为http协议加对应的端口号。
————————————————
版权声明:本文为CSDN博主「是豆腐啊」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a914541185/article/details/83181919

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏