Skip to content

Commit 3b5c16b

Browse files
authored
Merge pull request #87 from NerosoftDev/develop
Add more properties to JwtAuthenticationOptions.
2 parents 3aa51ec + 1c12b2e commit 3b5c16b

File tree

3 files changed

+70
-56
lines changed

3 files changed

+70
-56
lines changed

Source/Euonia.Hosting/Authentication/JwtAuthenticationOptions.cs

+44-34
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,48 @@ namespace Microsoft.AspNetCore.Authentication;
55
/// </summary>
66
public class JwtAuthenticationOptions
77
{
8-
/// <summary>
9-
/// Gets or sets the scheme.
10-
/// </summary>
11-
public string Scheme { get; set; }
12-
13-
/// <summary>
14-
/// Gets or sets the token issuers.
15-
/// </summary>
16-
public IEnumerable<string> Issuer { get; set; }
17-
18-
/// <summary>
19-
/// Gets or sets the signing key.
20-
/// </summary>
21-
public string SigningKey { get; set; }
22-
23-
/// <summary>
24-
/// Gets or sets the authority url.
25-
/// </summary>
26-
public string Authority { get; set; }
27-
28-
/// <summary>
29-
/// Gets or sets a value indicating whether the HTTPS metadata is required.
30-
/// </summary>
31-
public bool RequireHttpsMetadata { get; set; }
32-
33-
/// <summary>
34-
/// Gets or sets the audience.
35-
/// </summary>
36-
public string Audience { get; set; }
37-
38-
/// <summary>
39-
/// Gets or sets a value indicating if the policy should be used.
40-
/// </summary>
41-
public bool UsePolicy { get; set; } = true;
8+
/// <summary>
9+
/// Gets or sets the scheme.
10+
/// </summary>
11+
public string Scheme { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the token issuers.
15+
/// </summary>
16+
public IEnumerable<string> Issuer { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the signing key.
20+
/// </summary>
21+
public string SigningKey { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the authority url.
25+
/// </summary>
26+
public string Authority { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets a value indicating whether the HTTPS metadata is required.
30+
/// </summary>
31+
public bool RequireHttpsMetadata { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets the audience.
35+
/// </summary>
36+
public string Audience { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets a value indicating if the policy should be used.
40+
/// </summary>
41+
public bool UsePolicy { get; set; } = true;
42+
43+
/// <summary>
44+
/// Gets or sets a value indicating if the issuer should be validated.
45+
/// </summary>
46+
public bool ValidateIssuer { get; set; } = true;
47+
48+
/// <summary>
49+
/// Gets or sets a value indicating if the audience should be validated.
50+
/// </summary>
51+
public bool ValidateAudience { get; set; } = true;
4252
}

Source/Euonia.Hosting/Extensions/ServiceCollectionExtensions.cs

+25-21
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ public static IServiceCollection AddJwtAuthentication(this IServiceCollection se
4141
var bearerOptions = new JwtAuthenticationOptions();
4242
optionsAction?.Invoke(bearerOptions);
4343

44+
return services.AddJwtAuthentication(bearerOptions);
45+
}
46+
47+
/// <summary>
48+
///
49+
/// </summary>
50+
/// <param name="services"></param>
51+
/// <param name="configurationSectionName"></param>
52+
/// <returns></returns>
53+
public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, string configurationSectionName)
54+
{
55+
var bearerOptions = services.GetConfiguration().GetSection(configurationSectionName).Get<JwtAuthenticationOptions>();
56+
return services.AddJwtAuthentication(bearerOptions);
57+
}
58+
59+
/// <summary>
60+
/// Adds Jwt authentication to the DI container.
61+
/// </summary>
62+
/// <param name="services"></param>
63+
/// <param name="bearerOptions"></param>
64+
/// <returns></returns>
65+
public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, JwtAuthenticationOptions bearerOptions)
66+
{
4467
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
4568

4669
var key = Encoding.UTF8.GetBytes(bearerOptions.SigningKey);
@@ -107,33 +130,14 @@ public static IServiceCollection AddJwtAuthentication(this IServiceCollection se
107130
RoleClaimType = JwtClaimTypes.Role,
108131
ValidIssuers = bearerOptions.Issuer,
109132
//ValidAudience = "api",
110-
ValidateIssuer = false,
111-
ValidateAudience = false,
133+
ValidateIssuer = bearerOptions.ValidateIssuer,
134+
ValidateAudience = bearerOptions.ValidateAudience,
112135
IssuerSigningKey = new SymmetricSecurityKey(key)
113136
};
114137
});
115138
return services;
116139
}
117140

118-
/// <summary>
119-
///
120-
/// </summary>
121-
/// <param name="services"></param>
122-
/// <param name="configurationSectionName"></param>
123-
/// <returns></returns>
124-
public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, string configurationSectionName)
125-
{
126-
var bearerOptions = services.GetConfiguration().GetSection(configurationSectionName).Get<JwtAuthenticationOptions>();
127-
return services.AddJwtAuthentication(options =>
128-
{
129-
options.Audience = bearerOptions.Audience;
130-
options.Authority = bearerOptions.Authority;
131-
options.Issuer = bearerOptions.Issuer;
132-
options.RequireHttpsMetadata = bearerOptions.RequireHttpsMetadata;
133-
options.SigningKey = bearerOptions.SigningKey;
134-
});
135-
}
136-
137141
/// <summary>
138142
///
139143
/// </summary>

project.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>8.1.27</Version>
3+
<Version>8.1.28</Version>
44
<Authors>damon</Authors>
55
<Company>Nerosoft Ltd.</Company>
66
<Product>Euonia</Product>

0 commit comments

Comments
 (0)