Closed kmiskiewicz closed 6 years ago
I tried with this https://github.com/openiddict/openiddict-core/issues/467 unfortunately without success
@kmiskiewicz can you show me your angular code. Are you setting headers with bearer token for signal r to authenticate?
Sure :-), I've just created a fresh fork from the project to make sure that the problem is not caused by my changes, here is the commit with the example. https://github.com/kmiskiewicz/AspNetCoreSpa/commit/8b5e3a5263bfd72f242ef6063dcec1b7331f5338 I'd like to have the logged user id in the OnConnectedAsync() method. I didn't change anything more
Here is is how it works:
1) add validation handler:
code:
.AddOAuthValidation(options =>
{
options.Events.OnRetrieveToken = context =>
{
context.Token = context.Request.Query["access_token"];
return Task.CompletedTask;
};
})
import { Component, OnInit, Inject } from '@angular/core';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
import { AccountService } from '../../core/services/account.service';
@Component({ selector: 'appc-chat', templateUrl: './chat.component.html', styleUrls: ['./chat.component.scss'] }) export class ChatComponent implements OnInit {
private _hubConnection: HubConnection; public async: any; message = ''; messages: string[] = [];
constructor(@Inject('BASE_URL') private baseUrl: string, private accountService: AccountService) { }
public sendMessage(): void {
const data = Sent: ${this.message}
;
this._hubConnection.invoke('send', data);
this.messages.push(data);
this.message = '';
}
ngOnInit() {
console.log(this.accountService.accessToken);
this._hubConnection = new HubConnectionBuilder().withUrl(`${this.baseUrl}chathub?access_token=${this.accountService.accessToken}`).build();
this._hubConnection.on('send', (data: any) => {
const received = `Received: ${data}`;
this.messages.push(received);
});
this._hubConnection.start()
.then(() => {
console.log('Hub connection started');
})
.catch(err => {
console.log('Error while establishing connection: ' + err);
});
}
}
3. your ChatHub.cs code (which is exactly as you have got so far)
using System; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using System.Collections.Generic; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Http; using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using AspNetCoreSpa.Core.Entities;
namespace AspNetCoreSpa.Web.SignalR { public class Chat : Hub {
UserManager<ApplicationUser> _userManager;
public Chat(UserManager<ApplicationUser> userManager) : base()
{
_userManager = userManager;
}
public async Task Send(string message)
{
await Clients.All.SendAsync("Send", message);
}
public override async Task OnConnectedAsync()
{
var user = _userManager.GetUserAsync(Context.User).Result;
await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users"); //here instead of "SignalR Users" I'd like to use userId.
//Unfortunately the user is not authenticated (before I login in the application)
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users");
await base.OnDisconnectedAsync(exception);
}
}
}
@kmiskiewicz you should put Authorize attribute on hub. I faced with same issue, until you put this attribute, this code
.AddOAuthValidation(options =>
{
options.Events.OnRetrieveToken = context =>
{
context.Token = context.Request.Query["access_token"];
return Task.CompletedTask;
};
})
don't call. but you user will get unathorized if not signed. Based on that I have two hubs, and switching them based on user signed|anonymous state.
@parys of course if purpose is to make signalr hub secure you can put Authorize attribute. But it isn't a requirement to get authenticated user. The code that I pasted above still gives you user information without decorating Authorize.
@asadsahi I spent a lot of time, and maybe I did something wrong, but double checked all and without this attribute OnRetriveToken event don't appear, so user stayed anonymous
I tested the code and everything works like a charm, thank you so much @asadsahi , @parys ! Great help as always 👍
I hope you are well, I use your project as a base for my two personal projects and so far it works really great. It saved tons of time and It helped me to update my knowledge about the ASP CORE :-) Recently I started to use SignalR and I found an issue with hubs authentication.
Could you take a look or give me a hint how to get an information about the current user in SignalR Hubs?
Best regards, Krzysztof