特别说明:如果 Issue 报告为问题且开发成员回复确认问题之后但三天内都不能得到反馈,则视为无效Issue。
v1.15.0
根据在线文档,现在用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()
在vue3环境下可以正常使用signalr
== 这个是跨域没有配置,你得配置跨域。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
跨域我已经配成功了,现在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跨域不成功。
我遇到问题与肖骁一样的,请问兄台怎么解决的?
报错信息: 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()
);
我这边没用 , .net5
我的代码
登录 后才可以发表评论