@@ -73,14 +73,15 @@ public static void SetupApplicationCookie(this WebApplicationBuilder builder)
73
73
builder . Services . ConfigureApplicationCookie ( configure =>
74
74
{
75
75
configure . Cookie . Name = WEB_ApplicationCookieName ;
76
-
77
- builder . Environment . SetCookieOptions ( configure . Cookie ) ;
76
+ configure . Cookie . SecurePolicy = CookieSecurePolicy . Always ;
77
+ configure . Cookie . HttpOnly = true ;
78
+ configure . Cookie . SameSite = SameSiteMode . Strict ;
78
79
} ) ;
79
80
}
80
81
81
82
/// <summary>
82
83
/// Add Identity provider with custom <see cref="ApplicationUser"/> user and system <see cref="IdentityRole"/> role management.
83
- /// Add <see cref="ApplicationDbContext "/> ef store for the identities.
84
+ /// Add <see cref="AppDbContext "/> ef store for the identities.
84
85
/// Add default token providers.
85
86
/// </summary>
86
87
public static void SetupIdentityProvider ( this IServiceCollection serviceCollection ) => serviceCollection
@@ -101,11 +102,7 @@ public static List<string> GetRoles(this IEnumerable<Claim> claims) =>
101
102
claims . Where ( r => r . Type == ClaimTypes . Role ) . Select ( w => w . Value ) . ToList ( ) ;
102
103
103
104
/// <summary>
104
- /// Get JWT token validation parameters from given options and current configuration.
105
- /// <seealso cref="CONFIG_KEY_JwtSettings_Issuer"/>
106
- /// <seealso cref="CONFIG_KEY_JwtSettings_Audience"/>
107
- /// <seealso cref="CONFIG_KEY_JwtSettings_Key"/>
108
- /// <seealso cref="CONFIG_KEY_JwtSettings_ClockSkewSeconds"/>
105
+ /// Get JWT token validation parameters from given options and current configuration.
109
106
/// </summary>
110
107
/// <param name="validateIssuer">Will validate issuer (default: true).</param>
111
108
/// <param name="validateAudience">Will validate audience (default: true).</param>
@@ -189,8 +186,16 @@ userManager is not null && logger is not null &&
189
186
{
190
187
var opts = new CookieOptions ( ) ;
191
188
192
- hostEnvironment . SetCookieOptions ( builder . Configuration , opts , setExpiresAsRefreshToken : true ) ;
193
- context . HttpContext . Response . Cookies . Append ( WEB_CookieName_XAccessToken , res . AccessToken , opts ) ;
189
+ context . HttpContext . Response . Cookies . Append (
190
+ WEB_CookieName_XAccessToken ,
191
+ res . AccessToken ,
192
+ new CookieOptions
193
+ {
194
+ Secure = true ,
195
+ HttpOnly = true ,
196
+ SameSite = SameSiteMode . Strict ,
197
+ Expires = DateTimeOffset . UtcNow + builder . Configuration . GetAppConfig ( ) . Auth . Jwt . AccessTokenDuration
198
+ } ) ;
194
199
195
200
context . Principal = res . Principal ;
196
201
context . Success ( ) ;
@@ -211,37 +216,4 @@ userManager is not null && logger is not null &&
211
216
212
217
} ) ;
213
218
214
- /// <summary>
215
- /// Configure given CookieBuilder to set Secure, HttpOnly and Strict SameSite options on created cookies.
216
- /// </summary>
217
- public static void SetCookieOptions ( this IHostEnvironment environment , CookieBuilder cookieBuilder )
218
- {
219
- cookieBuilder . SecurePolicy = CookieSecurePolicy . Always ;
220
- cookieBuilder . HttpOnly = true ;
221
- cookieBuilder . SameSite = SameSiteMode . Strict ;
222
- }
223
-
224
- /// <summary>
225
- /// Configure given CookieOptions to set Secure, HttpOnly and Strict SameSite options on created cookies.
226
- /// </summary>
227
- /// <param name="environment"></param>
228
- /// <param name="configuration"></param>
229
- /// <param name="cookieOptions"></param>
230
- /// <param name="setExpiresAsRefreshToken">if true set expiration time as from JwtSettings:RefreshTokenDurationSeconds</param>
231
- public static void SetCookieOptions ( this IHostEnvironment environment , IConfiguration configuration ,
232
- CookieOptions cookieOptions , bool setExpiresAsRefreshToken = false )
233
- {
234
- cookieOptions . Secure = true ;
235
- cookieOptions . HttpOnly = true ;
236
- cookieOptions . SameSite = SameSiteMode . Strict ;
237
-
238
- if ( setExpiresAsRefreshToken )
239
- {
240
- var cookieDuration = configuration . GetAppConfig ( ) . Auth . Jwt . RefreshTokenDuration ;
241
-
242
- cookieOptions . Expires = DateTimeOffset . Now . Add ( cookieDuration ) ;
243
- }
244
- }
245
-
246
-
247
219
}
0 commit comments