@@ -11,10 +11,13 @@ namespace DocuSign.CodeExamples
1111    using  System . Net . Http ; 
1212    using  System . Net . Http . Headers ; 
1313    using  System . Security . Claims ; 
14+     using  System . Security . Cryptography ; 
15+     using  System . Text ; 
1416    using  System . Text . Json ; 
1517    using  System . Text . RegularExpressions ; 
1618    using  System . Threading . Tasks ; 
1719    using  System . Web ; 
20+     using  Azure . Core ; 
1821    using  DocuSign . CodeExamples . Common ; 
1922    using  DocuSign . CodeExamples . Models ; 
2023    using  DocuSign . Rooms . Api ; 
@@ -179,14 +182,17 @@ public void ConfigureServices(IServiceCollection services)
179182                options . ClaimActions . MapJsonKey ( "access_token" ,  "access_token" ) ; 
180183                options . ClaimActions . MapJsonKey ( "refresh_token" ,  "refresh_token" ) ; 
181184                options . ClaimActions . MapJsonKey ( "expires_in" ,  "expires_in" ) ; 
185+ 
182186                options . Events  =  new  OAuthEvents 
183187                { 
184188                    OnRedirectToAuthorizationEndpoint  =  redirectContext => 
185189                    { 
186190                        List < string >  scopesForCurrentApi  =  this . apiTypes . GetValueOrDefault ( Enum . Parse < ExamplesApiType > ( this . Configuration [ "API" ] ) ) ; 
187- 
188191                        redirectContext . RedirectUri  =  this . UpdateRedirectUriScopes ( redirectContext . RedirectUri ,  scopesForCurrentApi ) ; 
189192
193+                         redirectContext . Options . UsePkce  =  this . Configuration [ "PkceFailed" ]  ==  null ; 
194+ 
195+                         this . Configuration [ "RedirectUrl" ]  =  redirectContext . RedirectUri ; 
190196                        redirectContext . HttpContext . Response . Redirect ( redirectContext . RedirectUri ) ; 
191197                        return  Task . FromResult ( 0 ) ; 
192198                    } , 
@@ -206,11 +212,28 @@ public void ConfigureServices(IServiceCollection services)
206212                            context . RunClaimActions ( payload . RootElement ) ; 
207213                        } 
208214                    } , 
209-                     OnRemoteFailure  =  context => 
215+                     OnRemoteFailure  =  async   context => 
210216                    { 
211-                         context . HandleResponse ( ) ; 
212-                         context . Response . Redirect ( "/Home/Error?message="  +  context . Failure ? . Message ) ; 
213-                         return  Task . FromResult ( 0 ) ; 
217+                         if  ( this . Configuration [ "PkceFailed" ]  !=  null ) 
218+                         { 
219+                             context . HandleResponse ( ) ; 
220+                             context . Response . Redirect ( "/Home/Error?message="  +  context . Failure ? . Message ) ; 
221+                         } 
222+                         else 
223+                         { 
224+                             var  redirectContext  =  new  RedirectContext < OAuthOptions > ( 
225+                                 context . HttpContext , 
226+                                 context . Scheme , 
227+                                 options , 
228+                                 context . Properties , 
229+                                 this . Configuration [ "RedirectUrl" ] ) ; 
230+ 
231+                             this . Configuration [ "PkceFailed" ]  =  "true" ; 
232+ 
233+                             await  options . Events . OnRedirectToAuthorizationEndpoint ( redirectContext ) ; 
234+ 
235+                             context . HandleResponse ( ) ; 
236+                         } 
214237                    } , 
215238                } ; 
216239            } ) ; 
0 commit comments