[英]How to enable Cors in Signalr Azure Service
I am trying to use the Azure SignalR Service in a web app that only contains a hub class. 我试图在仅包含集线器类的Web应用程序中使用Azure SignalR服务。 When I try to access from another domain to the hub I get the following error
当我尝试从另一个域访问集线器时,我收到以下错误
"Access to XMLHttpRequest at 'https://*/genericSocketHub/negotiate' from origin ' https://localhost:44303 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.". “从原始' https:// localhost:44303 '访问'https:// * / genericSocketHub / negotiate'的XMLHttpRequest已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否'访问 - Control-Allow-Origin'标头出现在请求的资源上。“
In the startup.cs class of my project I have: 在我的项目的startup.cs类中,我有:
` public class Startup { public IConfiguration Configuration { get; `public class Startup {public IConfiguration Configuration {get; }
}
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors(o => o.AddPolicy("Policy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}));
services.AddSignalR().AddAzureSignalR(Configuration.GetConnectionString("AzureSignalRConnectionString")).AddJsonProtocol(options => options.PayloadSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = new DefaultContractResolver()});
services.AddSingleton(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCors("Policy");
app.UseAzureSignalR(routes =>
{
routes.MapHub<GenericSocketHub>("/genericSocketHub");
});
app.UseMvc();
}
}`
Without using Azure SignalR Service I didn't have any CORS issues 不使用Azure SignalR服务我没有任何CORS问题
Try adding .WithOrigins("[THE_DOMAIN_TO_UNBLOCK]");
尝试添加
.WithOrigins("[THE_DOMAIN_TO_UNBLOCK]");
to your policy: 对你的政策:
services.AddCors(o => o.AddPolicy("Policy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithOrigins("[THE_DOMAIN_TO_UNBLOCK]");
}));
Also make sure that you have the latest version of Microsoft.Asure.SignalR
installed on the server along with the latest @aspnet/signalr
installed on the client. 还要确保在服务器上安装了最新版本的
Microsoft.Asure.SignalR
以及客户端上安装的最新@aspnet/signalr
。
NOTE The signalr
npm package is not compatible with Azure SignalR. 注意
signalr
npm软件包与Azure SignalR不兼容。 I learned this the hard way.. 我经过惨痛的教训才学到这个..
The following worked for my setup which is Angular7, .NET CORE 2.1 and Azure SignalR. 以下适用于我的设置,即Angular7,.NET CORE 2.1和Azure SignalR。 My setup looks like this:
我的设置如下:
ConfigureServices ConfigureServices
// Add CORS
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyMethod()
.WithOrigins("http://localhost:4200");
});
});
// Add Azure SignalR
services.AddSignalR().AddAzureSignalR();
Configure 配置
app.UseCors("AllowAllOrigins");
app.UseAzureSignalR(routes =>
{
routes.MapHub<NextMatchHub>("/nextmatch");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller=Home}/{action=Index}/{id?}");
});
NOTE Make sure that the various implementations are added in the same order as my example shows above. 注意确保以与上面示例相同的顺序添加各种实现。 I cannot explain why it is sensitive about the order but this was also an issue on my end.
我无法解释为什么它对订单敏感,但这也是我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.