1
+ using System ;
1
2
using System . Linq ;
2
3
using Microsoft . AspNetCore . Authentication . Cookies ;
3
4
using Microsoft . AspNetCore . Authentication . OpenIdConnect ;
@@ -34,6 +35,28 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
34
35
{
35
36
loggerfactory . AddConsole ( LogLevel . Information ) ;
36
37
38
+ // Simple error page
39
+ app . Use ( async ( context , next ) =>
40
+ {
41
+ try
42
+ {
43
+ await next ( ) ;
44
+ }
45
+ catch ( Exception ex )
46
+ {
47
+ if ( ! context . Response . HasStarted )
48
+ {
49
+ context . Response . Clear ( ) ;
50
+ context . Response . StatusCode = 500 ;
51
+ await context . Response . WriteAsync ( ex . ToString ( ) ) ;
52
+ }
53
+ else
54
+ {
55
+ throw ;
56
+ }
57
+ }
58
+ } ) ;
59
+
37
60
app . UseIISPlatformHandler ( ) ;
38
61
39
62
app . UseCookieAuthentication ( new CookieAuthenticationOptions
@@ -52,17 +75,30 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
52
75
53
76
app . Run ( async context =>
54
77
{
78
+ if ( context . Request . Path . Equals ( "/signout" ) )
79
+ {
80
+ await context . Authentication . SignOutAsync ( CookieAuthenticationDefaults . AuthenticationScheme ) ;
81
+ context . Response . ContentType = "text/html" ;
82
+ await context . Response . WriteAsync ( $ "<html><body>Signing out { context . User . Identity . Name } <br>{ Environment . NewLine } ") ;
83
+ await context . Response . WriteAsync ( "<a href=\" /\" >Sign In</a>" ) ;
84
+ await context . Response . WriteAsync ( $ "</body></html>") ;
85
+ return ;
86
+ }
87
+
55
88
if ( ! context . User . Identities . Any ( identity => identity . IsAuthenticated ) )
56
89
{
57
90
await context . Authentication . ChallengeAsync ( OpenIdConnectDefaults . AuthenticationScheme , new AuthenticationProperties { RedirectUri = "/" } ) ;
58
-
59
- context . Response . ContentType = "text/plain" ;
60
- await context . Response . WriteAsync ( "Hello First timer" ) ;
61
91
return ;
62
92
}
63
93
64
- context . Response . ContentType = "text/plain" ;
65
- await context . Response . WriteAsync ( "Hello Authenticated User" ) ;
94
+ context . Response . ContentType = "text/html" ;
95
+ await context . Response . WriteAsync ( $ "<html><body>Hello Authenticated User { context . User . Identity . Name } <br>{ Environment . NewLine } ") ;
96
+ foreach ( var claim in context . User . Claims )
97
+ {
98
+ await context . Response . WriteAsync ( $ "{ claim . Type } : { claim . Value } <br>{ Environment . NewLine } ") ;
99
+ }
100
+ await context . Response . WriteAsync ( "<a href=\" /signout\" >Sign Out</a>" ) ;
101
+ await context . Response . WriteAsync ( $ "</body></html>") ;
66
102
} ) ;
67
103
}
68
104
0 commit comments