Skip to content

Commit 5411ce9

Browse files
author
Vadim Belov
committed
Support access token in cookies and centralize param name
Introduce AccessTokenParamName constant to avoid hardcoding the access token parameter name. Update JWT authentication to check for the token in both query string and cookies, improving flexibility and maintainability.
1 parent 0bfc56a commit 5411ce9

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

Sources/EasyExtensions.AspNetCore.Authorization/Extensions/ServiceCollectionExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace EasyExtensions.AspNetCore.Authorization.Extensions
2121
/// </summary>
2222
public static class ServiceCollectionExtensions
2323
{
24+
/// <summary>
25+
/// Represents the name of the parameter used to pass the access token.
26+
/// </summary>
27+
public const string AccessTokenParamName = "access_token";
28+
2429
/// <summary>
2530
/// Adds JWT authentication resolving <see cref="IConfiguration"/> from DI.
2631
/// Reads settings from JwtSettings section or flat fallback Jwt[Key] configuration values (see <see cref="ConfigurationExtensions.GetJwtSettings"/>).
@@ -92,11 +97,19 @@ public static IServiceCollection AddJwt(this IServiceCollection services)
9297
{
9398
OnMessageReceived = context =>
9499
{
95-
var accessToken = context.Request.Query["access_token"].ToString();
100+
var accessToken = context.Request.Query[AccessTokenParamName].ToString();
96101
if (!string.IsNullOrEmpty(accessToken))
97102
{
98103
context.Token = accessToken;
99104
}
105+
if (string.IsNullOrWhiteSpace(context.Token))
106+
{
107+
string? cookieToken = context.Request.Cookies[AccessTokenParamName];
108+
if (!string.IsNullOrEmpty(cookieToken))
109+
{
110+
context.Token = cookieToken;
111+
}
112+
}
100113
return Task.CompletedTask;
101114
}
102115
};

0 commit comments

Comments
 (0)