4
4
using Microsoft . AspNetCore . Hosting ;
5
5
using Microsoft . AspNetCore . Http ;
6
6
using Microsoft . Extensions . DependencyInjection ;
7
+ using Microsoft . Extensions . Logging ;
7
8
8
9
namespace ArchitectNow . Web . Configuration
9
10
{
10
- public class AntiForgeryStartupFilter : IStartupFilter
11
- {
12
- public Action < IApplicationBuilder > Configure ( Action < IApplicationBuilder > next )
13
- {
14
- return builder =>
15
- {
16
- var antiforgery = builder . ApplicationServices . GetService < IAntiforgery > ( ) ;
17
- builder . Use ( n => context =>
18
- {
19
- if ( string . Equals ( context . Request . Path . Value , "/" , StringComparison . OrdinalIgnoreCase ) ||
20
- string . Equals ( context . Request . Path . Value , "/index.html" , StringComparison . OrdinalIgnoreCase ) )
21
- {
22
- var tokens = antiforgery . GetAndStoreTokens ( context ) ;
23
- context . Response . Cookies . Append ( "X-XSRF-TOKEN" , tokens . RequestToken ,
24
- new CookieOptions { HttpOnly = false } ) ;
25
- }
11
+ public class AntiForgeryStartupFilter : IStartupFilter
12
+ {
13
+ private readonly ILogger < AntiForgeryStartupFilter > _logger ;
26
14
27
- return n ( context ) ;
28
- } ) ;
29
- next ( builder ) ;
30
- } ;
31
- }
32
- }
15
+ public AntiForgeryStartupFilter ( ILogger < AntiForgeryStartupFilter > logger )
16
+ {
17
+ _logger = logger ;
18
+ }
19
+
20
+ public Action < IApplicationBuilder > Configure ( Action < IApplicationBuilder > next )
21
+ {
22
+ return builder =>
23
+ {
24
+ _logger . LogInformation ( $ "Configure Start: { nameof ( AntiForgeryStartupFilter ) } ") ;
25
+
26
+ var antiforgery = builder . ApplicationServices . GetService < IAntiforgery > ( ) ;
27
+ builder . Use ( n => context =>
28
+ {
29
+ if ( string . Equals ( context . Request . Path . Value , "/" , StringComparison . OrdinalIgnoreCase ) ||
30
+ string . Equals ( context . Request . Path . Value , "/index.html" , StringComparison . OrdinalIgnoreCase ) )
31
+ {
32
+ var tokens = antiforgery . GetAndStoreTokens ( context ) ;
33
+ context . Response . Cookies . Append ( "X-XSRF-TOKEN" , tokens . RequestToken ,
34
+ new CookieOptions { HttpOnly = false } ) ;
35
+ }
36
+
37
+ return n ( context ) ;
38
+ } ) ;
39
+ next ( builder ) ;
40
+ _logger . LogInformation ( $ "Configure End: { nameof ( AntiForgeryStartupFilter ) } ") ;
41
+ } ;
42
+ }
43
+ }
33
44
}
0 commit comments