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

Why signing in the user not working after registration in Blazor Web App?

$
0
0

Here is my code in Blazor Web App using .NET 9.

I am posting this after trying for multiple days but unable to make it work.

I am not using ASP.NET Core Identity in this project.

Registration page with form:

    @page "/signup"<EditForm EditContext="@EditContext" OnValidSubmit="@Submit">        Registration form goes here</EditForm>    async Task Submit()    {       if (Model != null)       {             //create new user in db by calling the service          //assuming user record created in db          bool bInserted = true;          if (bInserted)          {             await Task.Delay(200);             NavigationManager.NavigateTo("/LoginAfterSignup", forceLoad: true);           }            }    }

AuthenticationEndPoints.cs:

public static class AuthenticationEndPoints{    public static void MapAuthenticationEndpoints(this WebApplication app)    {        // autologin        app.MapGet("/LoginAfterSignup", async (HttpContext context, IDbContextFactory<ApplicationDbContext> contextFactory) =>             {                await HandleSignUpCallback(context, contextFactory);             });    }    public static async Task HandleSignUpCallback(HttpContext context,           IDbContextFactory<ApplicationDbContext> contextFactory,           ClaimsPrincipal? principal = null,           string? redirectUri = null)    {        // lines removed for brevity        // sample claims        var claims = new List<Claim>            {                new Claim(ClaimTypes.NameIdentifier, "1"),                new Claim(ClaimTypes.Name, "Musa"),                new Claim(ClaimTypes.Email, "musa@bc.com"),                new Claim(ClaimTypes.Role, "User")            };        var authProperties = new AuthenticationProperties            {                IsPersistent = true, // Persist the cookie across browser sessions                ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7) // Set expiration for persistent cookie            };        var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);        var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);        // Signin the user to recreate the auth cookie        // This does not sign in the user with cookie but code runs        await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, authProperties);        context.Response.Redirect("/");    }}

Program.cs:

builder.Services.AddAuthentication(options =>{    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;}).AddCookie(options =>{   options.Cookie.Name = "BCAuth"; // Optional: Custom cookie name   options.LoginPath = "/Login"; // Path to your login page   options.AccessDeniedPath = "/AccessDenied"; // Optional: Path for access denied   options.Cookie.HttpOnly = true; // Important for security   options.ExpireTimeSpan = TimeSpan.FromDays(7); // Cookie expiration   options.SlidingExpiration = true; // Extend cookie on activity});

Once signup form is submitted, it hits the HandleSignUpCallback() correctly and executes all lines. But the user is not logged in nor the auth cookie is created.

What am I missing?

Thanks for reading and have a blessed day.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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