Search code examples
c#azureauthenticationazure-web-app-serviceauth0

User profile empty in HttpContext in azure web app


I am using an azure web app service with my identity provider Auth0. I got this working but whenever I login in my website hosted on azure the user profile is not loaded in HttpContext. This is working locally in IIS Express. Any idea what I might be missing?

This is my code to get the logged in user profile:

[Inject]
    private IHttpContextAccessor HttpContextAccessor { get; set; }
    private string loginId;

    /// <summary>
    /// On initialized
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await LoadInvoiceTypes();
        this.loginId = HttpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
    }

I did a bit of logging in the website and it seems that the HttpContext is null.

Thanks in advance!


Solution

  • Apparently it is advised in blazor to use the AuthenticationStateProvider. This is giving me the logged in user in azure as I expected it should be.

        protected string loginId;
    
        [Inject]
        private AuthenticationStateProvider AuthProvider { get; set; }
    
        protected override async Task OnInitializedAsync()
        {
            var authState = await AuthProvider.GetAuthenticationStateAsync();
            this.loginId = authState.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
            await base.OnInitializedAsync();
        }
    

    Thanks for your input.