CurrentUser.UserName is null in Blazor Server + SignalR project
- ABP Framework version: 4.3.0-rc.2
- User Interface type: Blazor Server (not tiered)
- Database provider: MongoDB
-
Creating a new Blazor WebAssembly project and adding SignalR-Integration works fine and the
CurrentUser.UserNameis populated:abp new Test -u blazor -d mongodb -v 4.3.0-rc.2 --previewhttps://github.com/Kobus-Smit/abp-blazor-wasm-signalr
-
but doing the same in a Blazor Server project, then
CurrentUser.UserNameis nullabp new Test -u blazor-server -d mongodb -v 4.3.0-rc.2 --previewhttps://github.com/Kobus-Smit/abp-blazor-server-signalr
PS: Many thanks for this great framework!
@cotur will try to reproduce it.
Maybe related to https://github.com/abpframework/abp/issues/8240 (while it is WASM)
It's reproducible,
By the way, @Kobus-Smit thanks so much for this well explained issue.
We should show this issue everywhere as a "How the bug report should be". :)
@cotur ;) No worries mate! I believe Minimal Reproducible Examples are always helpful.
@hikalkan , might be related, but with #8240 the username is available after refreshing the page, in contrast , CurrentUser.UserName stays null after refreshing the page in my sample projects.
Reading Microsoft's docs Authentication and authorization in ASP.NET Core SignalR it sounds like Cookie authentication should just work, no additional configuration needed?
Alternatively, Bearer token authentication can be used by setting the AccessTokenProvider
var connection = new HubConnectionBuilder()
.WithUrl("https://example.com/chathub", options =>
{
options.AccessTokenProvider = () => Task.FromResult(_myAccessToken);
})
.Build();
In WASM I can get the access token with IAccessTokenProvider.RequestAccessToken, but apparently not as straight forward with Blazor Server:
- Can't get the token from HttpContext
- as HttpContext is not available in a Blazor Server App
- From docs.microsoft.com: Additionally, again for security reasons, you must not use IHttpContextAccessor within Blazor apps. Blazor apps run outside of the context of the ASP.NET Core pipeline. The HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app
- I had a look at Pass tokens to a Blazor Server app but
HttpContext.GetTokenAsync("access_token")is null. Is theoptions.Scope.Add("offline_access")a clue? - Should I add
AddAbpOpenIdConnectin myBlazor Not Tieredas in theBlazor Server Tieredproject? Probably not? https://github.com/abpframework/abp/blob/62e1b238ced052f37f8c65695b867870b22768d8/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.Tiered/MyProjectNameBlazorModule.cs#L166
Other related links that might help:
- https://stackoverflow.com/questions/59891352/how-do-i-get-the-access-token-from-a-blazor-server-side-web-app
- https://stackoverflow.com/questions/59638965/how-to-add-openidconnect-via-identityserver4-to-asp-net-core-serverside-blazor-w
- https://github.com/enetstudio/SignalRServerIdentityAuthentication looks promising
It worked when I pass cookies to the server:
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR.Client;
namespace Test.Blazor.Pages
{
public partial class Index
{
[Inject]
public IHttpContextAccessor HttpContextAccessor { get; set; }
private HubConnection hubConnection;
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
if (CurrentUser.IsAuthenticated)
{
hubConnection = new HubConnectionBuilder()
.WithUrl("https://localhost:44313/signalr-hubs/messaging", options =>
{
if (HttpContextAccessor.HttpContext != null)
{
foreach (var cookie in HttpContextAccessor.HttpContext.Request.Cookies)
{
options.Cookies.Add(new Cookie(cookie.Key, cookie.Value, null, "localhost"));
}
}
}).Build();
await hubConnection.StartAsync();
}
}
public async ValueTask DisposeAsync()
{
if (hubConnection != null)
{
await hubConnection.DisposeAsync();
}
}
}
}
Result:

From the Microsoft's documentation, we should consider the second part, because in blazor app, we are using .NET Client. Request is not coming from browser (interestingly, server connects to itself):

However, I accessed the current HttpContext to get cookies. I am not sure where to get them (maybe from browser if somehow possible).
ABP's startup template uses cookie auth for blazor server app, so there is no bearer token that we can grab in the blazor app. Authentication stuff is done in the startup template actually, not related to the framework, so you can setup a different authentication method. We will continue to investigate alternative ways in the next milestone. If you've any suggestion, please write here. Thanks for your detailed descriptions.
Thanks @hikalkan , I'll pass the cookies to the server for now.
I am closing this. You can reopen if you need.