Can't negotiate signalr connection between .net core and angular

Viewed 1360

I'm trying to make a signalr connection between angular client and .net core server, but i'm getting the following exception on the server side:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Http.Connections.Internal.AuthorizeHelper.AuthorizeAsync(HttpContext context, IList`1 policies) at Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionDispatcher.ExecuteNegotiateAsync(HttpContext context, HttpConnectionDispatcherOptions options) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request.

My server configurations:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDistributedMemoryCache();
        services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddDbContext<StorigiContext>(options => 
            options.UseLazyLoadingProxies()
            .UseSqlServer(Configuration.GetConnectionString("StorigiContext")));
        services.AddSignalR();
        services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>{
                        builder
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .WithOrigins("http://localhost:4200");
                        }));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseSession();
        app.UseCors("CorsPolicy");
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{id?}"
            );
        });
        app.UseSignalR(routes =>
        {
            routes.MapHub<NotificationHub>("/hubs/notify");
        });

    }

Hub Class:

    public class NotificationHub : Hub
{
    private readonly static ConnectionMapper _connections = 
        new ConnectionMapper();

    public Task SendMessage(string message)
    {
        return Clients.All.SendAsync("sendNotification", message);
    }
}

The connection is made from a angular client: Like this:

public hubConnection: HubConnection;
public messages: string[] = [];
constructor(private service:AuthenticationServiceService, private router:Router) { }

ngOnInit() {

let builder = new HubConnectionBuilder();

// as per setup in the startup.cs
this.hubConnection = builder.withUrl('/hubs/notify')
                    .build();

// message coming from the server
this.hubConnection.on("sendNotification", (message) => {
  alert(message);
  this.messages.push(message);
});

// starting the connection
this.hubConnection.start();

}

I would like to know why is this exception occurring?

0 Answers
Related