© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•9mo ago•
1 reply
Cykotech

Blazor Cookie not updating AuthorizeView

I'm implementing a simple authorization through Blazor with a separate .NET API. The login form just takes in a password and returns 200 with a cookie or a 401. (This is for an admin dashboard so there will only ever be one user to login) The issue I'm having is that my AuthorizeView doesn't change the view to the Authorized element even though the cookie gets created.

// Admin.razor
@page "/Admin"
@using Microsoft.AspNetCore.Components.Authorization
@using BlazingBlog.Client.Components.Layout

<PageTitle>Admin</PageTitle>
<AuthorizeView>
    <Authorized>
        <main>
            <h2>Admin</h2>
            <PostEditForm/>
        </main>
    </Authorized>
    <NotAuthorized>
        <main>
            <LoginForm/>
        </main>
    </NotAuthorized>
</AuthorizeView>

// Login endpoint
            app.MapPost("api/login",
                        async (HttpContext context, [FromBody] string password) =>
                        {
                            Console.WriteLine($"Body: {password}");
                            if (password == "password")
                            {
                                var claims = new List<Claim>
                                {
                                    new(ClaimTypes.Role, "Admin")
                                };

                                ClaimsIdentity identity = new(claims,
                                                              CookieAuthenticationDefaults
                                                                  .AuthenticationScheme);
                                ClaimsPrincipal principal = new(identity);

                                await context.SignInAsync(
                                    CookieAuthenticationDefaults.AuthenticationScheme,
                                    principal);

                                return Results.Ok();
                            }

                            return Results.Unauthorized();
                        });

// Cookie config
            builder.Services
                   .AddAuthentication(CookieAuthenticationDefaults
                                          .AuthenticationScheme)
                   .AddCookie(options =>
                   {
                       options.Cookie.Name = "BlazingCookie";
                       options.ExpireTimeSpan = TimeSpan.FromHours(1);
                       options.SlidingExpiration = true;
                       options.Cookie.SameSite = SameSiteMode.None;
                       options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
                   });
            builder.Services.AddAuthorization();
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("BlazingClient", policy =>
                {
                    policy.WithOrigins("https://localhost:7071")
                          .AllowAnyHeader()
                          .AllowAnyMethod()
                          .AllowCredentials();
                });
            });
// Admin.razor
@page "/Admin"
@using Microsoft.AspNetCore.Components.Authorization
@using BlazingBlog.Client.Components.Layout

<PageTitle>Admin</PageTitle>
<AuthorizeView>
    <Authorized>
        <main>
            <h2>Admin</h2>
            <PostEditForm/>
        </main>
    </Authorized>
    <NotAuthorized>
        <main>
            <LoginForm/>
        </main>
    </NotAuthorized>
</AuthorizeView>

// Login endpoint
            app.MapPost("api/login",
                        async (HttpContext context, [FromBody] string password) =>
                        {
                            Console.WriteLine($"Body: {password}");
                            if (password == "password")
                            {
                                var claims = new List<Claim>
                                {
                                    new(ClaimTypes.Role, "Admin")
                                };

                                ClaimsIdentity identity = new(claims,
                                                              CookieAuthenticationDefaults
                                                                  .AuthenticationScheme);
                                ClaimsPrincipal principal = new(identity);

                                await context.SignInAsync(
                                    CookieAuthenticationDefaults.AuthenticationScheme,
                                    principal);

                                return Results.Ok();
                            }

                            return Results.Unauthorized();
                        });

// Cookie config
            builder.Services
                   .AddAuthentication(CookieAuthenticationDefaults
                                          .AuthenticationScheme)
                   .AddCookie(options =>
                   {
                       options.Cookie.Name = "BlazingCookie";
                       options.ExpireTimeSpan = TimeSpan.FromHours(1);
                       options.SlidingExpiration = true;
                       options.Cookie.SameSite = SameSiteMode.None;
                       options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
                   });
            builder.Services.AddAuthorization();
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("BlazingClient", policy =>
                {
                    policy.WithOrigins("https://localhost:7071")
                          .AllowAnyHeader()
                          .AllowAnyMethod()
                          .AllowCredentials();
                });
            });
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ <AuthorizeView> not working
C#CC# / help
3y ago
Blazor Page not updating model
C#CC# / help
2y ago
Updating element position in Blazor
C#CC# / help
2y ago
❔ blazor server app Authentication cookie or jwt token?
C#CC# / help
3y ago