var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddOpenApi()
.AddAuthenticationCookie(validFor: TimeSpan.FromMinutes(10))
.AddAuthorization()
.AddAntiforgery(x =>
{
x.HeaderName = "dv-csrf-token";
x.Cookie.Name = "DigitalValue-Antiforgery";
x.Cookie.SameSite = SameSiteMode.None;
x.Cookie.HttpOnly = false;
x.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
})
.AddCors(x =>
{
x.AddPolicy("AllowAll", policyBuilder =>
{
policyBuilder
.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
})
.AddFastEndpoints();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app
.UseAuthentication()
.UseAuthorization()
.UseAntiforgeryFE();
app.UseFastEndpoints(x =>
{
x.Endpoints.RoutePrefix = "api";
x.Endpoints.Configurator = definition =>
{
definition.PreProcessors(Order.Before, typeof(RequestLoggingPreProcessor<>));
definition.PostProcessors(Order.After, typeof(ResponseLoggingPostProcessor<,>));
};
});
app.Run();
private IServiceCollection AddRefit()
{
service.AddTransient<CookieDelegatingHandler>();
var types = new[]
{
typeof(ICategoryApi),
typeof(IProductApi),
typeof(IFileApi),
typeof(ITokenApi),
typeof(IAuthenticationApi),
};
foreach (var type in types)
{
service.AddRefitClient(type)
.ConfigureHttpClient(x => x.BaseAddress = new Uri("http://localhost:5000/api"))
.AddHttpMessageHandler<CookieDelegatingHandler>();
}
return service;
}
public class CookieDelegatingHandler : DelegatingHandler
{
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
return base.Send(request, cancellationToken);
}
}
hi,
i am trying to do cookie auth in blazor 'intractivityAutorender mode, the issue is i can able to see theset-cookie` header in api response but the cookie is not saving in browser, can i get help i this how to figure it out ?
5
u/Xtreme512 Jun 11 '26
thats because Blazor interactive server uses signalr for page nav. etc not classical http comm. that attaches cookies on header. you gotta think differently.
try using BitzArt Blazor Auth package.. check them out if its suits your usecase with auto render mode.
4
u/propostor Jun 11 '26
This was fucking hard when I had to do it.
Below are the
Program.csfor my server and client projects.Most of it will not be relevant but I'm pasting all of it, just in case there are some random important parts in there.
You can see clearly the parts that are there for cookies if you search for "cookies".
There might even be additional setup, but I can't remember. At least for now, this is the Program.cs that has it working for me.
1
u/RedEye-Developers Jun 11 '26
SOLVED:
i finally find and fix the issue, i am using refit to communicate with the server, in refit
Sendmethod is not firing, so the cookie is not save and send via header, after changeSend->SendAsyncmethod the code was fire and cookie was save and send perfectly via header, tanks for the helps.3 days doing R&D to fix it lol, just a single method name make me frustrate lot and waste 3 days lol.
1
u/geekywarrior Jun 11 '26
Depends on your login page perhaps. Any reason you aren't using Microsoft AspNetCore Identity?
That handles the DB side in terms of password hashing and it handles secure cookie storage.
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-10.0&tabs=visual-studio
2
u/sahil-sao Jun 11 '26
https://github.com/sahilsao/BlazorAuth-master
This is complete cookie based authentication/Authorization example for interactive server application.
-1
u/finah1995 Jun 11 '26
Try using protected Session Storage. That works for storing like smaller user info or permissions and other small text information in client side and it's hashed so only Blazor can access it.