Closed govi2010 closed 3 years ago
I see two issues in the logs - one seems related to CORS and one seems related to token authentication. Also, I don't know how is your CORS policy configured. I would recommend:
@moozzyk
I don't have any authentication on my hub.
//// No authorize
public class SignalHub : Hub
{
}
in my startup.cs file in public void ConfigureServices(IServiceCollection services)
method I have
services.AddCors(options => options.AddPolicy("Cors", builder =>
{
builder
.WithOrigins(
"http://localhost:4200",
"https://stage.blabla.com",
"http://stage.blabla.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
/// this is for only API not for SignalR
var signingKey =
new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(Configuration[Constants.ConfigurationKeys.Token.SigningKey]));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(config =>
{
config.RequireHttpsMetadata = false;
config.SaveToken = true;
config.TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = signingKey,
ValidateAudience = true,
ValidAudience = Configuration[Constants.ConfigurationKeys.Token.Audience],
ValidateIssuer = true,
ValidIssuer = Configuration[Constants.ConfigurationKeys.Token.Issuer],
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
config.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
{
context.Response.Headers.Add("Token-Expired", "true");
}
return Task.CompletedTask;
},
OnTokenValidated = async context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
var session = context.HttpContext.RequestServices.GetRequiredService<SessionManager>();
var claimsIdentity = context.Principal;
var email = claimsIdentity.Identity.Name;
}
};
});
services.AddAuthentication();
in my startup.cs file in public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
method I have
app.UseAuthentication(); /// this authentication is for API controller but not for signalrRhub
app.UseCors("Cors");
var webSocketOptions = new WebSocketOptions()
{
KeepAliveInterval = TimeSpan.FromSeconds(120),
ReceiveBufferSize = 4 * 1024
};
webSocketOptions.AllowedOrigins.Add("http://localhost:4200");
webSocketOptions.AllowedOrigins.Add("https://stage.blabla.com");
webSocketOptions.AllowedOrigins.Add("http://stage.blabla.com");
app.UseWebSockets(webSocketOptions);
app.UseCors("Cors");
@moozzyk I wonder why this same hub is working fine with JAVA client and JS client without authorization and without CORS issue,
I have not used the Java client nor I know if the code using it has done any additional configuration so I cannot comment on this. If the JS client is running in the browser it would have a bunch of headers filled out by default (including the Origin
header).
Looking at this:
services.AddCors(options => options.AddPolicy("Cors", builder =>
{
builder
.WithOrigins(
"http://localhost:4200",
"https://stage.blabla.com",
"http://stage.blabla.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
you allow only specific origins. The client is not setting any headers on your behalf. Likely, you need to set the Origin header to match any of the ones you allow.
@moozzyk you are right . I need to set Origin header.
Before filing an issue Check Frequently Asked Questions
Describe the bug
Below are server logs. Somehow I can getting 403 error there is not authorisation On server. JAVA client works well without authorisation.
It seems like CORS issue but I don't know what origin do i need to add on server side.
Expected behavior Connection ID is issued by server. but after that connectionDidFailToOpen method called instead of connectionDidOpen.
Code To Reproduce the Issue IOS Code is below. I am passing two query param.
Additional context Add any other context about the problem here.