When using SignalR core locally I am able to access my identity claims principle via the Hub using HubCallerContext, e.g:
string userIdentifier = Context.UserIdentifier
I achieve this by passing the .AspNetCore.Identity.Application cookie to the HubConnectionBuilder:
hubConnection = new HubConnectionBuilder()
.WithUrl(Navigation.ToAbsoluteUri("/chathub"), options =>
{
options.Cookies = GetAuthCookieContainer();
})
.Build();
await hubConnection.StartAsync();
private CookieContainer GetAuthCookieContainer()
{
string cookieName = ".AspNetCore.Identity.Application";
string domainName = new Uri(Navigation.BaseUri).Host;
CookieContainer cookieContainer = new CookieContainer();
if (HttpContext.HttpContext != null)
{
var token = HttpContext.HttpContext.Request.Cookies[cookieName];
if (token != null)
{
cookieContainer.Add(new Cookie(cookieName, token) { Domain = domainName });
}
}
return cookieContainer;
}
This stops working once I add Azure SignalR service to Program.cs:
builder.Services.AddSignalR().AddAzureSignalR();
This is because I cannot pass the identity cookie to HubConnectionBuilder as HttpContext is always null once Azure SignalR is added to DI. Apart from this SignalR Azure works as expected.
Does anyone have a solution to how I can pass AspNetCore.Identity.Application cookie to HubConnectionBuilder() when using Azure SignalR or is there a better approach to this problem?