Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to persist user session using ProtectedSessionStorage and Cookie based Authentication .NET8 Blazor WebApp

$
0
0

My application is using global InteractiveServerRenderMode without prerendering, but my Login component is using static SSR so that I can access HttpContext for cookie authentication. Below is how I set the rendermode in App.razor :

<HeadOutlet @rendermode="RenderModeForPage" />
<Routes @rendermode="RenderModeForPage" />

@code {    [CascadingParameter]    private HttpContext HttpContext { get; set; } = default!;    private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")        ? null        : new InteractiveServerRenderMode(false); } 


Below is the method that handles the authentication in my LogIn Component //Account/LogIn:

     public async Task HandleLogin()     {         isLoading = true; // Hide spinner         await Task.Delay(2000);         var results = await Authenticate(Input);         if (results.User is not null)         {             var claims = new List<Claim>                 {                     new Claim(ClaimTypes.NameIdentifier, results.User.PersonId.ToString()),                     new Claim(ClaimTypes.Name, results.User.Login.UserName),                     new Claim(ClaimTypes.Email,results.User.Email),                 };             var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);             var newUser = new ClaimsPrincipal(identity);             var authProperties = new AuthenticationProperties()             {                 AllowRefresh = true,                 ExpiresUtc = DateTime.UtcNow.AddMinutes(5),                 IsPersistent = Input.RememberMe,             };             //await ProtectedSessionStore.SetAsync("UserData", results);             await Context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, newUser, authProperties);                         RedirectManager.RedirectTo(ReturnUrl);         }         else         {             errorMessage = string.IsNullOrEmpty(results.Message) ? "Error: Invalid login credentials" : results.Message;         }     }


Since ProtectedSessionStore is not available in static SSR what's the ideal way of persisting the user data so I can access on other interactive components?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>