1212using Microsoft . Extensions . DependencyInjection ;
1313using Microsoft . Extensions . Hosting ;
1414using Newtonsoft . Json . Linq ;
15+
16+ using System . Threading . Tasks ;
1517using System ;
1618using System . Net . Http ;
1719using System . Net . Http . Headers ;
@@ -95,6 +97,12 @@ public void ConfigureServices(IServiceCollection services)
9597 options . ClaimActions . MapJsonKey ( "expires_in" , "expires_in" ) ;
9698 options . Events = new OAuthEvents
9799 {
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+ } ,
98106 OnCreatingTicket = async context =>
99107 {
100108 var request = new HttpRequestMessage ( HttpMethod . Get , context . Options . UserInformationEndpoint ) ;
@@ -113,12 +121,27 @@ public void ConfigureServices(IServiceCollection services)
113121 {
114122 context . RunClaimActions ( payload . RootElement ) ;
115123 }
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+ } ,
117140 } ;
118141 } ) ;
119142 }
120143
121- #nullable enable
144+ #nullable enable
122145 private string ? ExtractDefaultAccountValue ( JsonElement obj , string key )
123146 {
124147 if ( ! obj . TryGetProperty ( "accounts" , out var accounts ) )
@@ -166,7 +189,7 @@ public void ConfigureServices(IServiceCollection services)
166189
167190 return keyValue ;
168191 }
169- #nullable disable
192+ #nullable disable
170193
171194 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
172195 public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
0 commit comments