Search code examples
.netasp.net-coreauthentication.net-corehttpcontext

Accessing signed in user in dotnet 3.1 with HttpContextAccessor


Okey, so i'm getting impatient here. :) I'm trying to access my signed in user through HttpContext.User by accessing the IHttpContextAccessor but no user is available, nor any claims. Dotnet core 3.1 is being used here. This is basically how I understand it: Sign in user with HttpContext.SignInAsync(... and then the context is available through the IHttpContextAccessor outside controllers. Now, the context is available but cant find any accessible user information from the signin. I do see that the cookies are correctly attached to the requests but there is some transformation not being done. Does anyone know what I am missing?

            //My controller action:

            var claimsIdentity = new ClaimsIdentity("Application");
            claimsIdentity.AddClaim(emailClaim);
            ... more claims

            await HttpContext.SignInAsync(
                "Application",
                new ClaimsPrincipal(claimsIdentity)
            );


            // Startup.cs:ConfigureServices
            
             services.AddHttpContextAccessor();


            // In a MyClass

            MyClass(IHttpContextAccessor accessor)
            {
                accessor.HttpContext.Claims; // Nothing
            }

Solution

  • So, it looks like I configured my cookie authentication with google incorrectly. There might have been some issue with injecting MyClass.cs as singelton aswell so that is something to look out for.

    Setup based on: how to implement google login in .net core without an entityframework provider and https://dotnetthoughts.net/aspnetcore-social-authentication-without-identity/

    This is my working configuration:

                // Startup.cs:ConfigureServices
                services.AddAuthentication(v => {
                    v.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    v.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
                })
                .AddCookie()
                .AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
                {
                    IConfigurationSection googleAuthNSection =
                        Configuration.GetSection("Authentication:Google");
    
                    options.ClientId = googleAuthNSection["ClientId"];
                    options.ClientSecret = googleAuthNSection["ClientSecret"];
                });
                 
                // DIs accessor
                services.AddHttpContextAccessor();
    
    
                // AuthenticationController.cs:MyAction
    
                var authenticateResult = await HttpContext.AuthenticateAsync(GoogleDefaults.AuthenticationScheme);
    
                var claimsIdentity = new ClaimsIdentity("Application");
    
                var surnameClaim = authenticateResult.Principal.FindFirst(ClaimTypes.Surname);
    
                claimsIdentity.AddClaim(surnameClaim);
    
                await HttpContext.SignInAsync(
                   "Application",
                    new ClaimsPrincipal(claimsIdentity)
                );
    
            return LocalRedirect(returnUrl ?? "~/");
    
    
    
            // In MyClass.cs
    
            MyClass(IHttpContextAccessor accessor)
            {
                accessor.HttpContext.Claims; // Claims available!
            }