Skip to content

Commit bde0111

Browse files
committed
refactor: Extract method in Auth0
1 parent a829da6 commit bde0111

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/LinkDotNet.Blog.Web/Authentication/Auth0/Auth0Extensions.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public static void UseAuth0Authentication(this IServiceCollection services, ICon
2828
.AddCookie()
2929
.AddOpenIdConnect("Auth0", options =>
3030
{
31-
// Set the authority to your Auth0 domain
3231
var auth0 = configuration.GetSection("Auth0").Get<Auth0Information>();
3332
options.Authority = $"https://{auth0.Domain}";
3433
options.ClientId = auth0.ClientId;
@@ -48,33 +47,33 @@ public static void UseAuth0Authentication(this IServiceCollection services, ICon
4847

4948
options.Events = new OpenIdConnectEvents
5049
{
51-
// handle the logout redirection
52-
OnRedirectToIdentityProviderForSignOut = context =>
53-
{
54-
var logoutUri = $"https://{auth0.Domain}/v2/logout?client_id={auth0.ClientId}";
55-
56-
var postLogoutUri = context.Properties.RedirectUri;
57-
if (!string.IsNullOrEmpty(postLogoutUri))
58-
{
59-
if (postLogoutUri.StartsWith("/"))
60-
{
61-
// transform to absolute
62-
var request = context.Request;
63-
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
64-
}
65-
66-
logoutUri += $"&returnTo={Uri.EscapeDataString(postLogoutUri)}";
67-
}
68-
69-
context.Response.Redirect(logoutUri);
70-
context.HandleResponse();
71-
72-
return Task.CompletedTask;
73-
},
50+
OnRedirectToIdentityProviderForSignOut = context => HandleRedirect(auth0, context),
7451
};
7552
});
7653

7754
services.AddHttpContextAccessor();
7855
services.AddScoped<ILoginManager, Auth0LoginManager>();
7956
}
80-
}
57+
58+
private static Task HandleRedirect(Auth0Information auth0, RedirectContext context)
59+
{
60+
var logoutUri = $"https://{auth0.Domain}/v2/logout?client_id={auth0.ClientId}";
61+
62+
var postLogoutUri = context.Properties.RedirectUri;
63+
if (!string.IsNullOrEmpty(postLogoutUri))
64+
{
65+
if (postLogoutUri.StartsWith("/"))
66+
{
67+
var request = context.Request;
68+
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
69+
}
70+
71+
logoutUri += $"&returnTo={Uri.EscapeDataString(postLogoutUri)}";
72+
}
73+
74+
context.Response.Redirect(logoutUri);
75+
context.HandleResponse();
76+
77+
return Task.CompletedTask;
78+
}
79+
}

0 commit comments

Comments
 (0)