1+ using System ;
2+ using System . Threading . Tasks ;
13using Microsoft . AspNetCore . Authentication . Cookies ;
24using Microsoft . AspNetCore . Authentication . OpenIdConnect ;
35using Microsoft . AspNetCore . Builder ;
46using Microsoft . AspNetCore . Http ;
7+ using Microsoft . Extensions . Configuration ;
58using Microsoft . Extensions . DependencyInjection ;
69
710namespace LinkDotNet . Blog . Web . Authentication . OpenIdConnect ;
811
912public static class AuthExtensions
1013{
11- public static void UseAuthentication ( this IServiceCollection services )
14+ public static void UseAuthentication ( this IServiceCollection services , IConfiguration configuration )
1215 {
16+ ArgumentNullException . ThrowIfNull ( configuration ) ;
17+
18+ var authInformation = configuration . GetSection ( AuthInformation . AuthInformationSection ) . Get < AuthInformation > ( ) ;
19+ if ( authInformation == null )
20+ {
21+ throw new InvalidOperationException ( "Authentication configuration is missing." ) ;
22+ }
23+
1324 services . Configure < CookiePolicyOptions > ( options =>
1425 {
1526 options . CheckConsentNeeded = _ => false ;
@@ -23,9 +34,52 @@ public static void UseAuthentication(this IServiceCollection services)
2334 options . DefaultChallengeScheme = CookieAuthenticationDefaults . AuthenticationScheme ;
2435 } )
2536 . AddCookie ( )
26- . AddOpenIdConnect ( ) ;
37+ . AddOpenIdConnect ( authInformation . Provider , options =>
38+ {
39+ options . Authority = $ "https://{ authInformation . Domain } ";
40+ options . ClientId = authInformation . ClientId ;
41+ options . ClientSecret = authInformation . ClientSecret ;
42+
43+ options . ResponseType = "code" ;
44+
45+ options . Scope . Clear ( ) ;
46+ options . Scope . Add ( "openid" ) ;
47+ options . Scope . Add ( "profile" ) ;
48+
49+ // Set the callback path, so Auth provider will call back to http://localhost:1234/callback
50+ // Also ensure that you have added the URL as an Allowed Callback URL in your Auth provider dashboard
51+ options . CallbackPath = new PathString ( "/callback" ) ;
52+
53+ // Configure the Claims Issuer to be Auth provider
54+ options . ClaimsIssuer = authInformation . Provider ;
55+
56+ options . Events = new OpenIdConnectEvents
57+ {
58+ OnRedirectToIdentityProviderForSignOut = async context => await HandleRedirect ( authInformation , context ) ,
59+ } ;
60+ } ) ;
2761
2862 services . AddHttpContextAccessor ( ) ;
2963 services . AddScoped < ILoginManager , AuthLoginManager > ( ) ;
3064 }
65+
66+ private static Task HandleRedirect ( AuthInformation auth , RedirectContext context )
67+ {
68+ var postLogoutUri = context . Properties . RedirectUri ;
69+ if ( ! string . IsNullOrEmpty ( postLogoutUri ) )
70+ {
71+ if ( postLogoutUri . StartsWith ( '/' ) )
72+ {
73+ var request = context . Request ;
74+ postLogoutUri = request . Scheme + "://" + request . Host + request . PathBase + postLogoutUri ;
75+ }
76+
77+ auth . LogoutUri += $ "&returnTo={ Uri . EscapeDataString ( postLogoutUri ) } ";
78+ }
79+
80+ context . Response . Redirect ( auth . LogoutUri ) ;
81+ context . HandleResponse ( ) ;
82+
83+ return Task . CompletedTask ;
84+ }
3185}
0 commit comments