@@ -319,7 +319,8 @@ public void Register(IAppHost appHost)
319319 appHost . Register < IAuthMetadataProvider > ( new AuthMetadataProvider ( ) ) ;
320320
321321 appHost . CustomErrorHttpHandlers [ HttpStatusCode . Unauthorized ] = new AuthFeatureUnauthorizedHttpHandler ( this ) ;
322- appHost . CustomErrorHttpHandlers [ HttpStatusCode . Forbidden ] = new AuthFeatureForbiddenHttpHandler ( this ) ;
322+ appHost . CustomErrorHttpHandlers [ HttpStatusCode . Forbidden ] = new AuthFeatureAccessDeniedHttpHandler ( this ) ;
323+ appHost . CustomErrorHttpHandlers [ HttpStatusCode . PaymentRequired ] = new AuthFeatureAccessDeniedHttpHandler ( this ) ;
323324
324325 AuthProviders . OfType < IAuthPlugin > ( ) . Each ( x => x . Register ( appHost , this ) ) ;
325326
@@ -401,6 +402,9 @@ public static string GetHtmlRedirect(this AuthFeature feature)
401402
402403 return "~/" + HostContext . ResolveLocalizedString ( LocalizedStrings . Login ) ;
403404 }
405+
406+ public static string GetHtmlRedirectUrl ( this AuthFeature feature , IRequest req ) =>
407+ feature . GetHtmlRedirectUrl ( req , feature . HtmlRedirectAccessDenied ?? feature . HtmlRedirect , includeRedirectParam : true ) ;
404408
405409 public static string GetHtmlRedirectUrl ( this AuthFeature feature , IRequest req , string redirectUrl , bool includeRedirectParam )
406410 {
@@ -423,6 +427,12 @@ public static string GetHtmlRedirectUrl(this AuthFeature feature, IRequest req,
423427 return url ;
424428 }
425429
430+ public static void DoHtmlRedirect ( this AuthFeature feature , string redirectUrl , IRequest req , IResponse res , bool includeRedirectParam )
431+ {
432+ var url = feature . GetHtmlRedirectUrl ( req , redirectUrl , includeRedirectParam ) ;
433+ res . RedirectToUrl ( url ) ;
434+ }
435+
426436 private static string ToQueryString ( NameValueCollection queryStringCollection )
427437 {
428438 if ( queryStringCollection == null || queryStringCollection . Count == 0 )
@@ -431,9 +441,8 @@ private static string ToQueryString(NameValueCollection queryStringCollection)
431441 return "?" + queryStringCollection . ToFormUrlEncoded ( ) ;
432442 }
433443
434-
435444 //http://stackoverflow.com/questions/3588623/c-sharp-regex-for-a-username-with-a-few-restrictions
436- public static Regex ValidUserNameRegEx = new Regex ( @"^(?=.{3,20}$)([A-Za-z0-9][._-]?)*$" , RegexOptions . Compiled ) ;
445+ public static Regex ValidUserNameRegEx = new ( @"^(?=.{3,20}$)([A-Za-z0-9][._-]?)*$" , RegexOptions . Compiled ) ;
437446
438447 public static bool IsValidUsername ( this AuthFeature feature , string userName )
439448 {
@@ -489,6 +498,18 @@ public static IHttpResult SuccessAuthResult(this IHttpResult result, IServiceBas
489498 }
490499 return result ;
491500 }
501+
502+ public static Task HandleFailedAuth ( this IAuthProvider authProvider ,
503+ IAuthSession session , IRequest httpReq , IResponse httpRes )
504+ {
505+ if ( authProvider is AuthProvider baseAuthProvider )
506+ return baseAuthProvider . OnFailedAuthentication ( session , httpReq , httpRes ) ;
507+
508+ httpRes . StatusCode = ( int ) HttpStatusCode . Unauthorized ;
509+ httpRes . AddHeader ( HttpHeaders . WwwAuthenticate , $ "{ authProvider . Provider } realm=\" { authProvider . AuthRealm } \" ") ;
510+ return HostContext . AppHost . HandleShortCircuitedErrors ( httpReq , httpRes , httpReq . Dto ) ;
511+ }
512+
492513 }
493514
494515 public class AuthFeatureUnauthorizedHttpHandler : HttpAsyncTaskHandler
@@ -498,32 +519,27 @@ public class AuthFeatureUnauthorizedHttpHandler : HttpAsyncTaskHandler
498519
499520 public override Task ProcessRequestAsync ( IRequest req , IResponse res , string operationName )
500521 {
501- if ( feature . HtmlRedirectAccessDenied != null && req . ResponseContentType . MatchesContentType ( MimeTypes . Html ) )
522+ if ( feature . HtmlRedirect != null && req . ResponseContentType . MatchesContentType ( MimeTypes . Html ) )
502523 {
503524 var url = feature . GetHtmlRedirectUrl ( req , feature . HtmlRedirect , includeRedirectParam : true ) ;
504525 res . RedirectToUrl ( url ) ;
505526 return TypeConstants . EmptyTask ;
506527 }
507528
508529 var iAuthProvider = feature . AuthProviders . First ( ) ;
509- if ( iAuthProvider is AuthProvider authProvider )
510- return authProvider . OnFailedAuthentication ( null , req , res ) ;
511- if ( iAuthProvider is AuthProviderSync authProviderSync )
512- return authProviderSync . OnFailedAuthentication ( null , req , res ) ;
513-
514530 res . StatusCode = ( int ) HttpStatusCode . Unauthorized ;
515531 res . AddHeader ( HttpHeaders . WwwAuthenticate , $ "{ iAuthProvider . Provider } realm=\" { iAuthProvider . AuthRealm } \" ") ;
516- return HostContext . AppHost . HandleShortCircuitedErrors ( req , res , req . Dto ) ;
532+ return res . EndHttpHandlerRequestAsync ( ) ;
517533 }
518534
519535 public override bool IsReusable => true ;
520536 public override bool RunAsAsync ( ) => true ;
521537 }
522538
523- public class AuthFeatureForbiddenHttpHandler : ForbiddenHttpHandler
539+ public class AuthFeatureAccessDeniedHttpHandler : ForbiddenHttpHandler
524540 {
525541 private readonly AuthFeature feature ;
526- public AuthFeatureForbiddenHttpHandler ( AuthFeature feature ) => this . feature = feature ;
542+ public AuthFeatureAccessDeniedHttpHandler ( AuthFeature feature ) => this . feature = feature ;
527543
528544 public override Task ProcessRequestAsync ( IRequest req , IResponse res , string operationName )
529545 {
@@ -537,5 +553,4 @@ public override Task ProcessRequestAsync(IRequest req, IResponse res, string ope
537553 }
538554 }
539555
540-
541556}
0 commit comments