5.9K Star 11.7K Fork 4K

GVPdotNET China / Furion

 / 详情

vue3环境下signalR跨域报错

已完成
创建于  
2021-03-09 12:43

特别说明:如果 Issue 报告为问题且开发成员回复确认问题之后但三天内都不能得到反馈,则视为无效Issue。

Furion 版本号

v1.15.0


Web 项目类型

  • WebApi
  • Mvc
  • Razor Pages
  • Blazor Server

描述你的问题

根据在线文档,现在用axios已经不报跨域错误了,但是signalr还是会报跨域错误。


异常堆栈信息

Access to XMLHttpRequest at 'https://10.235.48.119:5001/hubs/cardReader/negotiate' from origin 'http://localhost:8080' 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.


代码或代码仓库

import "../../node_modules/signalr/jquery.signalR.min.js";
const signalR = require("@aspnet/signalr");

let connection = new signalR.HubConnectionBuilder()
  .withUrl("https://10.235.48.119:5001/hubs/cardReader")
  .build();

connection.start()

数据库信息

  • Sqlite
  • SqlServer
  • Mysql
  • Oracle
  • PGSql
  • Firebird
  • Cosmos

期待结果

在vue3环境下可以正常使用signalr


评论 (13)

肖骁 创建了任务
肖骁 关联仓库设置为dotNET China/Furion
展开全部操作日志

== 这个是跨域没有配置,你得配置跨域。

百小僧 任务状态待办的 修改为进行中
百小僧 任务状态进行中 修改为已完成
百小僧 负责人设置为百小僧
百小僧 添加了
 
疑问
标签
百小僧 添加了
 
重复工单
标签
百小僧 里程碑设置为Furion 2021
百小僧 关联分支设置为master
百小僧 计划截止日期设置为2021-03-10
百小僧 计划开始日期设置为2021-03-09
百小僧 计划截止日期2021-03-10 修改为2021-03-09

跨域我已经配成功了,现在API调用没问题,但是signalr被跨域拦截了

肖骁 任务状态已完成 修改为进行中

startup

using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace VendingMachine.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();

            services.AddControllersWithViews()
                .AddInjectWithUnifyResult();
            services.AddSignalR();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHubs();
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

静态页面代码

<button onclick="sendMessage()">send</button>
<script src="https://cdn.bootcdn.net/ajax/libs/microsoft-signalr/5.0.1/signalr.min.js"></script>
<script>
let connection      = new signalR.HubConnectionBuilder().withUrl(" https://10.235.48.119:5001/hubs/cardReader").build();
     connection.on("cardRead", function (content) {
       alert(content)
     });
     connection.start();

function sendMessage(){
             connection.invoke("SendMessage", "sdfgsdfg");
}
</script>

我现在的状态是api跨域成功,signalr本地成功,signalr跨域不成功。

根据官方文档
是应该要有.AllowCredentials();

终于解决了

services.AddSignalR();
services.AddCorsAccessor()
app.UseCorsAccessor(builder => 
    builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials()
);
肖骁 任务状态进行中 修改为已完成
肖骁 关联分支master 修改为未关联

我遇到问题与肖骁一样的,请问兄台怎么解决的?
报错信息: has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

api 跨域问题已解决,只有signalr 被阻止,是否cookies 验证导致?

上面已经解答了

app.UseCorsAccessor(builder => 
    builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials()
);

我没用你这个框架的,看官方文档也配置好像是对的。很棘手。

没用 Furion 那来这里 Issue 。。。。。

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(5)
974299 monksoul 1578937227 805126 shawnshowxiao 1644564422 73073 jerryfox 1661931359 7342044 huguodong520 1695866989
C#
1
https://gitee.com/dotnetchina/Furion.git
git@gitee.com:dotnetchina/Furion.git
dotnetchina
Furion
Furion

搜索帮助