Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Claims from User in signalR hub web api .net core

I want to add chat real-time feature to my Web project. I'm using asp.net SignalR 2.4 to implement that feature. But i struggle to get UserId from bearer token in hub class.

I provide token from client side using angular 7 and i can get that token via query string in hub class.

I can easily get UserId in one of my controller using:

var userId = User.Claims.First(c => c.Type == "UserID").Value;

But in hub class User is always null Code from my hub class:

public override Task OnConnectedAsync()
{
    var identity = (ClaimsIdentity)Context.User.Identity;
    var connectionId = Context.ConnectionId;
    var query = Context.GetHttpContext().Request.Query;
    var token = query["access_token"].ToString();
    return base.OnConnectedAsync();
}

Code from client-side using angular 7:

ngOnInit(): void {
    this.hubConnection = new signalR.HubConnectionBuilder()
      .withUrl('http://localhost:50792/chat',{
        accessTokenFactory: () => {
          return this.token;
        },
      } as signalR.IHttpConnectionOptions)
      .build();
    // this.hubConnection.Headers.Add("Authorization", "Bearer " +
this.token);
    this.hubConnection
      .start()
      .then(() => console.log('Connection started'))
}
like image 541
Jonathan Avatar asked Nov 17 '22 10:11

Jonathan


1 Answers

You have to hook the OnMessageReceived event in order to allow the JWT authentication handler to read the access token from the query string.

builder.Services.AddAuthentication(options =>
{
    // ...
}).AddJwtBearer(options =>
  {
      // ...

      options.Events = new JwtBearerEvents
      {
          OnMessageReceived = context =>
          {
              var accessToken = context.Request.Query["access_token"];   
              var path = context.HttpContext.Request.Path;

              if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/chat"))
              {
                  context.Token = accessToken;
              }

              return Task.CompletedTask;
          }
      };
  });

From Microsoft documentation.

like image 88
Dimitris Maragkos Avatar answered Dec 09 '22 15:12

Dimitris Maragkos