Search code examples
c#azureasp.net-corecloudblazor

Net Core 3.0 Linux Azure Web App IHttpContextAccessor have null context


I have an issue in production with my Blazor Server application and the IHttpContextAccessor with Identity.

When I try to access the HttpContext, this one is null in the Azure Web App but not in my local machine.

I use this gateway to access the Context :

public class AuthenticationGateway : IAuthenticationGateway
    {
        private IHttpContextAccessor _httpContextAccessor;
        private UserManager<User> _userManager;
        public AuthenticationGateway(IHttpContextAccessor httpContextAccessor, UserManager<User> userManager)
        {
            _httpContextAccessor = httpContextAccessor;
            _userManager = userManager;
        }

        public bool IsLogguedIn()
        {
            return _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated;
        }

        public async Task<User> GetLogguedUser()
        {
            return await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
        }
    }

I use a B1 plan webapp under Linux working with Kestrel.


Solution

  • Please add the below code in the Startup.ConfigureServices method

    services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();