Skip to content

Commit 100519d

Browse files
committed
Allow disabling Secure cookie flag
1 parent 7f1143e commit 100519d

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/BitzArt.Blazor.Auth.Server/Extensions/ServerSideAddBlazorAuthExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,37 @@ public static class ServerSideAddBlazorAuthExtensions
1616
/// using default implementations of <see cref="IAuthenticationService"/> and <see cref="IIdentityClaimsService"/>.
1717
/// </summary>
1818
/// <param name="builder">The <see cref="IHostApplicationBuilder"/> to add services to.</param>
19+
/// <param name="configure">An <see cref="Action"/> to configure <see cref="BlazorAuthServerOptions"/>.</param>
1920
/// <returns><see cref="IHostApplicationBuilder"/> to allow chaining.</returns>
20-
public static IHostApplicationBuilder AddBlazorAuth(this IHostApplicationBuilder builder)
21+
public static IHostApplicationBuilder AddBlazorAuth(this IHostApplicationBuilder builder, Action<BlazorAuthServerOptions>? configure = null)
2122
{
22-
return builder.AddBlazorAuth<DefaultAuthenticationService, IdentityClaimsService>();
23+
return builder.AddBlazorAuth<DefaultAuthenticationService, IdentityClaimsService>(configure);
2324
}
2425

2526
/// <summary>
2627
/// Adds server-side Blazor.Auth services to the specified <see cref="IHostApplicationBuilder"/>, <br />
2728
/// using the default implementation of <see cref="IIdentityClaimsService"/>.
2829
/// </summary>
2930
/// <typeparam name="TAuthenticationService">The type of the server-side authentication service.</typeparam>
30-
public static IHostApplicationBuilder AddBlazorAuth<TAuthenticationService>(this IHostApplicationBuilder builder)
31+
public static IHostApplicationBuilder AddBlazorAuth<TAuthenticationService>(this IHostApplicationBuilder builder, Action<BlazorAuthServerOptions>? configure = null)
3132
where TAuthenticationService : class, IAuthenticationService
3233
{
33-
return builder.AddBlazorAuth<TAuthenticationService, IdentityClaimsService>();
34+
return builder.AddBlazorAuth<TAuthenticationService, IdentityClaimsService>(configure);
3435
}
3536

3637
/// <summary>
3738
/// Adds server-side Blazor.Auth services to the specified <see cref="IHostApplicationBuilder"/>.
3839
/// </summary>
3940
/// <typeparam name="TAuthenticationService">The type of the server-side authentication service.</typeparam>
4041
/// <typeparam name="TIdentityClaimsService">The type of the identity claims service.</typeparam>
41-
public static IHostApplicationBuilder AddBlazorAuth<TAuthenticationService, TIdentityClaimsService>(this IHostApplicationBuilder builder)
42+
public static IHostApplicationBuilder AddBlazorAuth<TAuthenticationService, TIdentityClaimsService>(this IHostApplicationBuilder builder, Action<BlazorAuthServerOptions>? configure = null)
4243
where TAuthenticationService : class, IAuthenticationService
4344
where TIdentityClaimsService : class, IIdentityClaimsService
4445
{
46+
var options = new BlazorAuthServerOptions();
47+
configure?.Invoke(options);
48+
builder.Services.AddSingleton(options);
49+
4550
builder.AddBlazorCookies();
4651
builder.Services.AddScoped<IBlazorAuthLogger, BlazorAuthLogger>();
4752

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BitzArt.Blazor.Auth.Server;
2+
3+
/// <summary>
4+
/// Options for the Blazor Auth Server.
5+
/// </summary>
6+
public class BlazorAuthServerOptions
7+
{
8+
/// <summary>
9+
/// Allows the app to operate in a non-HTTPS environment.
10+
/// </summary>
11+
public bool DisableSecureCookieFlag { get; set; } = false;
12+
}

src/BitzArt.Blazor.Auth.Server/Services/StaticUserService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ internal class StaticUserService(
1010
IBlazorAuthLogger logger,
1111
IAuthenticationService authService,
1212
ICookieService cookieService,
13-
IIdentityClaimsService claimsService
13+
IIdentityClaimsService claimsService,
14+
BlazorAuthServerOptions options
1415
) : IUserService
1516
{
1617
private protected static AuthenticationState UnauthorizedState => new(new ClaimsPrincipal());
@@ -78,13 +79,15 @@ private protected async Task SaveJwtPairAsync(JwtPair? jwtPair, CancellationToke
7879
{
7980
if (jwtPair is null) return;
8081

82+
var secure = !options.DisableSecureCookieFlag;
83+
8184
if (!string.IsNullOrWhiteSpace(jwtPair.AccessToken))
8285
await cookieService.SetAsync(
8386
Cookies.AccessToken,
8487
jwtPair.AccessToken!,
8588
jwtPair.AccessTokenExpiresAt,
8689
httpOnly: true,
87-
secure: true,
90+
secure: secure,
8891
sameSiteMode: SameSiteMode.Strict,
8992
cancellationToken: cancellationToken);
9093

@@ -94,7 +97,7 @@ await cookieService.SetAsync(
9497
jwtPair.RefreshToken!,
9598
jwtPair.RefreshTokenExpiresAt,
9699
httpOnly: true,
97-
secure: true,
100+
secure: secure,
98101
sameSiteMode: SameSiteMode.Strict,
99102
cancellationToken: cancellationToken);
100103
}

0 commit comments

Comments
 (0)