Skip to content

Commit aefca91

Browse files
refactor: Added IOptions #273 (#318)
* Added Ioption * Changed app settings * Removed AuthInformation from application settings * Removed Introduction from ApplicationConfiguration * Removed Social From App Configuration * fixed Sorting of usings * Moved Profile Information * Moved Giscus and Disqus * minor changes, added space and changed the extension name * moved configuration
1 parent d50c44e commit aefca91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+468
-326
lines changed

docs/Authorization/Auth0.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ For more information go to: https://auth0.com/docs/applications
66

77
### Configuration
88

9-
In `appsettings.json` change the `AuthenticationProvider` to `Auth0`
9+
In `appsettings.json` change the `Authentication:Provider` to `Auth0`
1010
and add following configurations
1111

1212
```json
1313
{
1414
//other configuration
15-
"AuthenticationProvider": "Auth0",
16-
"Auth0": {
15+
,
16+
"Authentication": {
17+
"Provider": "Auth0",
1718
"Domain": "",
1819
"ClientId": "",
1920
"ClientSecret": ""
@@ -24,7 +25,7 @@ and add following configurations
2425

2526
| Property | Type | Description |
2627
| --------------------------- | ------ | ------------------------------------------------------------------------------------------------------- |
27-
| AuthenticationProvider | | Name of the auth provider |
28+
| Authentication:Provider | | Name of the auth provider |
2829
| `name of the auth provider` | | Configuration for setting up the auth provider, it should be same as the value of AuthProvider property |
2930
| Domain | string | See more details here: https://manage.auth0.com/dashboard/ |
3031
| ClientId | string | See more details here: https://manage.auth0.com/dashboard/ |

docs/Authorization/AzureAD.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ and add following configurations
2525
```json
2626
{
2727
//other configuration
28-
"AuthenticationProvider": "AzureAD",
29-
"AzureAD": {
28+
29+
"Authentication": {
30+
"Provider": "AzureAD",
3031
"Domain": "",
3132
"ClientId": "",
3233
"ClientSecret": "",
@@ -38,7 +39,7 @@ and add following configurations
3839

3940
| Property | Type | Description |
4041
| --------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------- |
41-
| AuthenticationProvider | | Name of the auth provider |
42+
| Authentication:Provider | | Name of the auth provider |
4243
| `name of the auth provider` | | Configuration for setting up the auth provider, it should be same as the value of AuthProvider property |
4344
| Domain | string | `login.microsoftonline.com/<TENANT_ID>/v2.0` |
4445
| ClientId | string | Application (client) ID |

docs/Setup/Configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ The appsettings.json file has a lot of options to customize the content of the b
2222
"PersistenceProvider": "InMemory",
2323
"ConnectionString": "",
2424
"DatabaseName": "",
25-
"AuthProvider": "PROVIDER_NAME",
26-
"PROVIDER_NAME": {
25+
"Authentication": {
26+
"Provider": "PROVIDER_NAME",
2727
"Domain": "",
2828
"ClientId": "",
2929
"ClientSecret": "",
3030
"LogoutUri": ""
3131
},
3232
"BlogPostsPerPage": 10,
33-
"AboutMeProfileInformation": {
33+
"ProfileInformation": {
3434
"Name": "Steven Giesel",
3535
"Heading": "Software Engineer",
3636
"ProfilePictureUrl": "assets/profile-picture.webp"

src/LinkDotNet.Blog.Domain/Introduction.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
public sealed record Introduction
44
{
5+
public const string IntroductionSection = "Introduction";
6+
57
public string BackgroundUrl { get; init; }
68

79
public string ProfilePictureUrl { get; init; }
810

911
public string Description { get; init; }
10-
}
12+
}

src/LinkDotNet.Blog.Domain/ProfileInformation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public sealed record ProfileInformation
44
{
5+
public const string ProfileInformationSection = "ProfileInformation";
56
public string Name { get; init; }
67

78
public string Heading { get; init; }

src/LinkDotNet.Blog.Domain/Social.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace LinkDotNet.Blog.Domain;
22

33
public sealed record Social
44
{
5+
public const string SocialSection = "Social";
6+
57
public string LinkedinAccountUrl { get; init; }
68

79
public bool HasLinkedinAccount => !string.IsNullOrEmpty(LinkedinAccountUrl);

src/LinkDotNet.Blog.Web/AppConfigurationFactory.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/LinkDotNet.Blog.Web/AppConfiguration.cs renamed to src/LinkDotNet.Blog.Web/ApplicationConfiguration.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,22 @@
44

55
namespace LinkDotNet.Blog.Web;
66

7-
public sealed record AppConfiguration
7+
public sealed record ApplicationConfiguration
88
{
99
public string BlogName { get; init; }
1010

1111
public string BlogBrandUrl { get; init; }
1212

13-
public Introduction Introduction { get; init; }
14-
15-
public Social Social { get; init; }
16-
1713
public string ConnectionString { get; init; }
1814

1915
public string DatabaseName { get; init; }
2016

21-
public int BlogPostsPerPage { get; init; }
22-
23-
public bool IsAboutMeEnabled => ProfileInformation != null;
24-
25-
public ProfileInformation ProfileInformation { get; init; }
17+
public int BlogPostsPerPage { get; init; } = 10;
2618

27-
public GiscusConfiguration GiscusConfiguration { get; init; }
19+
public bool IsAboutMeEnabled { get; set; }
2820

29-
public bool IsGiscusEnabled => GiscusConfiguration != null;
30-
31-
public DisqusConfiguration DisqusConfiguration { get; init; }
32-
33-
public bool IsDisqusEnabled => DisqusConfiguration != null;
21+
public bool IsGiscusEnabled { get; set; }
22+
public bool IsDisqusEnabled { get; set; }
3423

3524
public string KofiToken { get; init; }
3625

@@ -45,8 +34,4 @@ public sealed record AppConfiguration
4534
public string PatreonName { get; init; }
4635

4736
public bool IsPatreonEnabled => !string.IsNullOrEmpty(PatreonName);
48-
49-
public string AuthenticationProvider { get; set; }
50-
51-
public AuthInformation AuthInformation { get; set; }
5237
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Options;
89

910
namespace LinkDotNet.Blog.Web.Authentication.OpenIdConnect;
1011

1112
public static class AuthExtensions
1213
{
13-
public static void UseAuthentication(this IServiceCollection services, AppConfiguration appConfiguration)
14+
public static void UseAuthentication(this IServiceCollection services)
1415
{
15-
ArgumentNullException.ThrowIfNull(appConfiguration);
16+
var authInformation = services.BuildServiceProvider().GetService<IOptions<AuthInformation>>();
1617

1718
services.Configure<CookiePolicyOptions>(options =>
1819
{
@@ -27,11 +28,11 @@ public static void UseAuthentication(this IServiceCollection services, AppConfig
2728
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2829
})
2930
.AddCookie()
30-
.AddOpenIdConnect(appConfiguration.AuthenticationProvider, options =>
31+
.AddOpenIdConnect(authInformation.Value.Provider, options =>
3132
{
32-
options.Authority = $"https://{appConfiguration.AuthInformation.Domain}";
33-
options.ClientId = appConfiguration.AuthInformation.ClientId;
34-
options.ClientSecret = appConfiguration.AuthInformation.ClientSecret;
33+
options.Authority = $"https://{authInformation.Value.Domain}";
34+
options.ClientId = authInformation.Value.ClientId;
35+
options.ClientSecret = authInformation.Value.ClientSecret;
3536

3637
options.ResponseType = "code";
3738

@@ -43,11 +44,11 @@ public static void UseAuthentication(this IServiceCollection services, AppConfig
4344
options.CallbackPath = new PathString("/callback");
4445

4546
// Configure the Claims Issuer to be Auth provider
46-
options.ClaimsIssuer = appConfiguration.AuthenticationProvider;
47+
options.ClaimsIssuer = authInformation.Value.Provider;
4748

4849
options.Events = new OpenIdConnectEvents
4950
{
50-
OnRedirectToIdentityProviderForSignOut = context => HandleRedirect(appConfiguration.AuthInformation, context),
51+
OnRedirectToIdentityProviderForSignOut = context => HandleRedirect(authInformation.Value, context),
5152
};
5253
});
5354

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@ namespace LinkDotNet.Blog.Web.Authentication.OpenIdConnect;
22

33
public sealed record AuthInformation
44
{
5+
private string logoutUrl = string.Empty;
6+
7+
public const string AuthInformationSection = "Authentication";
8+
9+
public string Provider { get; set; }
10+
511
public string Domain { get; init; }
612

713
public string ClientId { get; init; }
814

915
public string ClientSecret { get; init; }
1016

11-
public string LogoutUri { get; set; }
17+
public string LogoutUri
18+
{
19+
get => !string.IsNullOrEmpty(logoutUrl) ? logoutUrl : $"https://{Domain}/v2/logout?client_id={ClientId}";
20+
set => logoutUrl = value;
21+
}
1222
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Authentication;
44
using Microsoft.AspNetCore.Authentication.Cookies;
55
using Microsoft.AspNetCore.Http;
6+
using Microsoft.Extensions.Options;
67

78
namespace LinkDotNet.Blog.Web.Authentication.OpenIdConnect;
89

@@ -11,13 +12,13 @@ public sealed class AuthLoginManager : ILoginManager
1112
private readonly HttpContext httpContext;
1213
private readonly string authProvider;
1314

14-
public AuthLoginManager(IHttpContextAccessor httpContextAccessor, AppConfiguration appConfiguration)
15+
public AuthLoginManager(IHttpContextAccessor httpContextAccessor, IOptions<AuthInformation> authInformation)
1516
{
1617
ArgumentNullException.ThrowIfNull(httpContextAccessor);
17-
ArgumentNullException.ThrowIfNull(appConfiguration);
18+
ArgumentNullException.ThrowIfNull(authInformation);
1819

1920
httpContext = httpContextAccessor.HttpContext ?? throw new ArgumentNullException(nameof(httpContextAccessor));
20-
authProvider = appConfiguration.AuthenticationProvider;
21+
authProvider = authInformation.Value.Provider;
2122
}
2223

2324
public async Task SignInAsync(string redirectUri)

0 commit comments

Comments
 (0)