abp icon indicating copy to clipboard operation
abp copied to clipboard

CurrentUser.UserName is null in Blazor Server + SignalR project

Open Kobus-Smit opened this issue 5 years ago • 8 comments

  • ABP Framework version: 4.3.0-rc.2
  • User Interface type: Blazor Server (not tiered)
  • Database provider: MongoDB
  1. Creating a new Blazor WebAssembly project and adding SignalR-Integration works fine and the CurrentUser.UserName is populated: abp new Test -u blazor -d mongodb -v 4.3.0-rc.2 --preview https://github.com/Kobus-Smit/abp-blazor-wasm-signalr image

  2. but doing the same in a Blazor Server project, then CurrentUser.UserName is null abp new Test -u blazor-server -d mongodb -v 4.3.0-rc.2 --preview https://github.com/Kobus-Smit/abp-blazor-server-signalr image

PS: Many thanks for this great framework!

Kobus-Smit avatar Apr 19 '21 10:04 Kobus-Smit

@cotur will try to reproduce it.

ebicoglu avatar Apr 19 '21 21:04 ebicoglu

Maybe related to https://github.com/abpframework/abp/issues/8240 (while it is WASM)

hikalkan avatar Apr 19 '21 23:04 hikalkan

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 avatar Apr 19 '21 23:04 cotur

@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.

Kobus-Smit avatar Apr 20 '21 01:04 Kobus-Smit

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 the options.Scope.Add("offline_access") a clue?
  • Should I add AddAbpOpenIdConnect in my Blazor Not Tiered as in the Blazor Server Tiered project? 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

Kobus-Smit avatar Apr 20 '21 01:04 Kobus-Smit

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:

image

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):

image

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.

hikalkan avatar Apr 20 '21 23:04 hikalkan

Thanks @hikalkan , I'll pass the cookies to the server for now.

Kobus-Smit avatar Apr 21 '21 01:04 Kobus-Smit

I am closing this. You can reopen if you need.

hikalkan avatar May 11 '21 01:05 hikalkan