Skip to content

Commit dafc344

Browse files
committed
fix: Setup for Auth
1 parent 7467e1a commit dafc344

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/LinkDotNet.Blog.Web/Authentication/OpenIdConnect/AuthExtensions.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
using System;
2+
using System.Threading.Tasks;
13
using Microsoft.AspNetCore.Authentication.Cookies;
24
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
35
using Microsoft.AspNetCore.Builder;
46
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Configuration;
58
using Microsoft.Extensions.DependencyInjection;
69

710
namespace LinkDotNet.Blog.Web.Authentication.OpenIdConnect;
811

912
public 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
}

src/LinkDotNet.Blog.Web/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static void RegisterServices(WebApplicationBuilder builder)
5858
}
5959
else
6060
{
61-
builder.Services.UseAuthentication();
61+
builder.Services.UseAuthentication(builder.Configuration);
6262
}
6363
}
6464

0 commit comments

Comments
 (0)