Javascript 使用identity server 4匿名访问信号器集线器
使用IdentityServer4时,我无法访问我的信号器端点(从javascript) 设置identity server 4安装+信号器。不幸的是,我不完全理解日志输出Javascript 使用identity server 4匿名访问信号器集线器,javascript,.net,signalr,identityserver4,Javascript,.net,Signalr,Identityserver4,使用IdentityServer4时,我无法访问我的信号器端点(从javascript) 设置identity server 4安装+信号器。不幸的是,我不完全理解日志输出 CORS request made for path: /hubs/SampleMessageHub from origin: > http://localhost:4200 but was ignored because path was not for an allowed IdentityServer COR
CORS request made for path: /hubs/SampleMessageHub from origin: >
http://localhost:4200 but was ignored because path was not for an allowed
IdentityServer CORS endpoint
Startup.cs
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment _environment { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_environment = env;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddSignalR();
services.AddControllersWithViews();
services.AddIdentityServer(options =>
{
options.Events.RaiseSuccessEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseErrorEvents = true;
})
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddDeveloperSigningCredential()
.AddCustomUserStore();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseCors("MyPolicy");
app.UseWebSockets();
app.UseStaticFiles();
app.UseIdentityServer();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<SampleHub>("/hubs/SampleMessageHub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
我在角度上的错误是:
[2019-10-02T09:35:27.872Z] Information: WebSocket connected to wss://localhost:5001/hubs/SampleMessageHub.
core.js:4002 ERROR Error: Error parsing handshake response: TypeError: Right-hand side of 'instanceof' is not callable
at HubConnection.push../node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processHandshakeResponse (HubConnection.js:372)
at HubConnection.push../node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processIncomingData (HubConnection.js:322)
at WebSocketTransport.HubConnection.connection.onreceive (HubConnection.js:65)
at WebSocket.webSocket.onmessage (WebSocketTransport.js:107)
at WebSocket.wrapFn (zone.js:1279)
at ZoneDelegate.invokeTask (zone.js:431)
at Object.onInvokeTask (core.js:26246)
at ZoneDelegate.invokeTask (zone.js:430)
at Zone.runTask (zone.js:198)
at ZoneTask.invokeTask [as invoke] (zone.js:513)
如果我在没有identity server的情况下执行一个新项目,那么一切都会正常工作。不知何故,需要对信号器端点进行一些配置——但这种配置看起来如何
提前感谢您的帮助。请尝试在
GetClients()
方法中添加AllowedCorsOrigins
属性。
e、 g
public IEnumerable GetClients()
{
返回新列表
{
新客户
{
允许的科索里金人=
{
"http://localhost:4200/",
},
//其余的财产
}
}
}
原因是现在您允许CORS域(localhost:4200)访问您的API。尝试在
GetClients()
方法中添加AllowedCorsOrigins
属性。
e、 g
public IEnumerable GetClients()
{
返回新列表
{
新客户
{
允许的科索里金人=
{
"http://localhost:4200/",
},
//其余的财产
}
}
}
原因是现在您允许CORS域(localhost:4200)访问您的API
public class SampleHub : Hub<ISampleHub>
{
public SampleHub()
{
}
Task SendMessage(string message)
{
Console.WriteLine("Message received: " + message);
return Task.CompletedTask;
}
}
dbug: IdentityServer4.Hosting.CorsPolicyProvider[0]
CORS request made for path: /hubs/SampleMessageHub from origin:
http://localhost:4200 but was ignored because path was not for an
allowed IdentityServer CORS endpoint
info: Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware[10]
No CORS policy found for the specified request.
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint '/hubs/SampleMessageHub'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint '/hubs/SampleMessageHub'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 309.1009ms 101
[2019-10-02T09:35:27.872Z] Information: WebSocket connected to wss://localhost:5001/hubs/SampleMessageHub.
core.js:4002 ERROR Error: Error parsing handshake response: TypeError: Right-hand side of 'instanceof' is not callable
at HubConnection.push../node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processHandshakeResponse (HubConnection.js:372)
at HubConnection.push../node_modules/@aspnet/signalr/dist/esm/HubConnection.js.HubConnection.processIncomingData (HubConnection.js:322)
at WebSocketTransport.HubConnection.connection.onreceive (HubConnection.js:65)
at WebSocket.webSocket.onmessage (WebSocketTransport.js:107)
at WebSocket.wrapFn (zone.js:1279)
at ZoneDelegate.invokeTask (zone.js:431)
at Object.onInvokeTask (core.js:26246)
at ZoneDelegate.invokeTask (zone.js:430)
at Zone.runTask (zone.js:198)
at ZoneTask.invokeTask [as invoke] (zone.js:513)
public IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
AllowedCorsOrigins =
{
"http://localhost:4200/",
},
// Rest of the properties
}
}
}