12
12
using Microsoft . Extensions . DependencyInjection ;
13
13
using Microsoft . Extensions . Hosting ;
14
14
using Newtonsoft . Json . Linq ;
15
+
16
+ using System . Threading . Tasks ;
15
17
using System ;
16
18
using System . Net . Http ;
17
19
using System . Net . Http . Headers ;
@@ -95,6 +97,12 @@ public void ConfigureServices(IServiceCollection services)
95
97
options . ClaimActions . MapJsonKey ( "expires_in" , "expires_in" ) ;
96
98
options . Events = new OAuthEvents
97
99
{
100
+ OnRedirectToAuthorizationEndpoint = redirectContext =>
101
+ {
102
+ redirectContext . Options . UsePkce = this . Configuration [ "PkceFailed" ] == null ;
103
+ redirectContext . HttpContext . Response . Redirect ( redirectContext . RedirectUri ) ;
104
+ return Task . FromResult ( 0 ) ;
105
+ } ,
98
106
OnCreatingTicket = async context =>
99
107
{
100
108
var request = new HttpRequestMessage ( HttpMethod . Get , context . Options . UserInformationEndpoint ) ;
@@ -113,12 +121,27 @@ public void ConfigureServices(IServiceCollection services)
113
121
{
114
122
context . RunClaimActions ( payload . RootElement ) ;
115
123
}
116
- }
124
+ } ,
125
+ OnRemoteFailure = async context =>
126
+ {
127
+ var redirectContext = new RedirectContext < OAuthOptions > (
128
+ context . HttpContext ,
129
+ context . Scheme ,
130
+ options ,
131
+ context . Properties ,
132
+ this . Configuration [ "RedirectUrl" ] ) ;
133
+
134
+ this . Configuration [ "PkceFailed" ] = "true" ;
135
+
136
+ await options . Events . OnRedirectToAuthorizationEndpoint ( redirectContext ) ;
137
+
138
+ context . HandleResponse ( ) ;
139
+ } ,
117
140
} ;
118
141
} ) ;
119
142
}
120
143
121
- #nullable enable
144
+ #nullable enable
122
145
private string ? ExtractDefaultAccountValue ( JsonElement obj , string key )
123
146
{
124
147
if ( ! obj . TryGetProperty ( "accounts" , out var accounts ) )
@@ -166,7 +189,7 @@ public void ConfigureServices(IServiceCollection services)
166
189
167
190
return keyValue ;
168
191
}
169
- #nullable disable
192
+ #nullable disable
170
193
171
194
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
172
195
public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
0 commit comments