Skip to content

Commit a877f5d

Browse files
committed
- Fixed issue with resolving of IBasicUserValidationService implementation when using multiple schemes
- Updated version - Updated samples
1 parent 30dbf69 commit a877f5d

File tree

10 files changed

+36
-60
lines changed

10 files changed

+36
-60
lines changed

samples/SampleWebApi_2_0/Properties/launchSettings.json

+2-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:3920/",
7-
"sslPort": 0
6+
"applicationUrl": "http://localhost:3920",
7+
"sslPort": 44304
88
}
99
},
1010
"profiles": {
@@ -15,15 +15,6 @@
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
18-
},
19-
"SampleWebApi": {
20-
"commandName": "Project",
21-
"launchBrowser": true,
22-
"launchUrl": "api/values",
23-
"environmentVariables": {
24-
"ASPNETCORE_ENVIRONMENT": "Development"
25-
},
26-
"applicationUrl": "http://localhost:3921/"
2718
}
2819
}
2920
}

samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
15+
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
1616
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
1717
</ItemGroup>
1818

samples/SampleWebApi_2_2/Properties/launchSettings.json

+2-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"$schema": "http://json.schemastore.org/launchsettings.json",
33
"iisSettings": {
44
"windowsAuthentication": false,
5-
"anonymousAuthentication": true,
5+
"anonymousAuthentication": true,
66
"iisExpress": {
7-
"applicationUrl": "http://localhost:1938",
7+
"applicationUrl": "http://localhost:3920",
88
"sslPort": 44304
99
}
1010
},
@@ -16,15 +16,6 @@
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development"
1818
}
19-
},
20-
"SampleWebApi_2_2": {
21-
"commandName": "Project",
22-
"launchBrowser": true,
23-
"launchUrl": "api/values",
24-
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25-
"environmentVariables": {
26-
"ASPNETCORE_ENVIRONMENT": "Development"
27-
}
2819
}
2920
}
3021
}

samples/SampleWebApi_2_2/SampleWebApi_2_2.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
9+
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
1010
<PackageReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1212
</ItemGroup>

samples/SampleWebApi_3_1/Properties/launchSettings.json

+2-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:2756",
7-
"sslPort": 44301
6+
"applicationUrl": "http://localhost:3920",
7+
"sslPort": 44304
88
}
99
},
1010
"$schema": "http://json.schemastore.org/launchsettings.json",
@@ -16,15 +16,6 @@
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development"
1818
}
19-
},
20-
"SampleWebApi_3_1": {
21-
"commandName": "Project",
22-
"launchBrowser": true,
23-
"launchUrl": "weatherforecast",
24-
"environmentVariables": {
25-
"ASPNETCORE_ENVIRONMENT": "Development"
26-
},
27-
"applicationUrl": "https://localhost:5001;http://localhost:5000"
2819
}
2920
}
3021
}

samples/SampleWebApi_3_1/SampleWebApi_3_1.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
88

99
<ItemGroup>
10-
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
10+
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
1111
</ItemGroup>
1212

1313
<!--<ItemGroup>

src/AspNetCore.Authentication.Basic/AspNetCore.Authentication.Basic.csproj

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp3.1;netcoreapp3.0;netstandard2.0;net461</TargetFrameworks>
5-
<Version>3.1.0</Version>
6-
<RepositoryUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/3.1.0</RepositoryUrl>
7-
<PackageProjectUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/3.1.0</PackageProjectUrl>
5+
<Version>3.1.1</Version>
6+
<RepositoryUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</RepositoryUrl>
7+
<PackageProjectUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</PackageProjectUrl>
88
<PackageTags>aspnetcore, security, authentication, microsoft, microsoft.aspnetcore.authentication, microsoft-aspnetcore-authentication, microsoft.aspnetcore.authentication.basic, microsoft-aspnetcore-authentication-basic, asp-net-core, netstandard, netstandard20, basic-authentication, basicauthentication, dotnetcore, dotnetcore3.1, asp-net-core-basic-authentication, aspnetcore-basic-authentication, asp-net-core-authentication, aspnetcore-authentication, asp, aspnet, basic, authentication-scheme</PackageTags>
9-
<PackageReleaseNotes>- Multitarget framework support added
10-
- Source Link support added
11-
- Strong Name Key support added
12-
- SuppressWWWAuthenticateHeader added to configure options
13-
- Events added to configure options
9+
<PackageReleaseNotes>- Fixed issue with resolving of IBasicUserValidationService implementation when using multiple schemes
1410
</PackageReleaseNotes>
1511
<Description>Easy to use and very light weight Microsoft style Basic Scheme Authentication implementation for ASP.NET Core.</Description>
1612
<Authors>Mihir Dilip</Authors>

src/AspNetCore.Authentication.Basic/BasicExtensions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class BasicExtensions
1616
{
1717
/// <summary>
1818
/// Adds basic authentication scheme to the project.
19-
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/> if an implementation of <see cref="IBasicUserValidationService"/> is not registered in the dependency container.
19+
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/>.
2020
/// </summary>
2121
/// <param name="builder"></param>
2222
/// <returns>The instance of <see cref="AuthenticationBuilder"/></returns>
@@ -25,7 +25,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder)
2525

2626
/// <summary>
2727
/// Adds basic authentication scheme to the project.
28-
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/> if an implementation of <see cref="IBasicUserValidationService"/> is not registered in the dependency container.
28+
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/>.
2929
/// </summary>
3030
/// <param name="builder"></param>
3131
/// <param name="authenticationScheme">The authentication scheme.</param>
@@ -35,7 +35,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,
3535

3636
/// <summary>
3737
/// Adds basic authentication scheme to the project.
38-
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/> if an implementation of <see cref="IBasicUserValidationService"/> is not registered in the dependency container.
38+
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
3939
/// </summary>
4040
/// <param name="builder"></param>
4141
/// <param name="configureOptions">The configure options.</param>
@@ -45,7 +45,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,
4545

4646
/// <summary>
4747
/// Adds basic authentication scheme to the project.
48-
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/> if an implementation of <see cref="IBasicUserValidationService"/> is not registered in the dependency container.
48+
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
4949
/// </summary>
5050
/// <param name="builder"></param>
5151
/// <param name="authenticationScheme">The authentication scheme.</param>
@@ -56,7 +56,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,
5656

5757
/// <summary>
5858
/// Adds basic authentication scheme to the project.
59-
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/> if an implementation of <see cref="IBasicUserValidationService"/> is not registered in the dependency container.
59+
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
6060
/// </summary>
6161
/// <param name="builder"></param>
6262
/// <param name="authenticationScheme">The authentication scheme.</param>

src/AspNetCore.Authentication.Basic/BasicHandler.cs

+16-9
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,28 @@ private async Task<AuthenticateResult> RaiseAndHandleAuthenticationSucceededAsyn
196196

197197
private async Task<bool> ValidateUsingBasicUserValidationServiceAsync(string username, string password)
198198
{
199-
var basicUserValidationService = Context.RequestServices.GetService<IBasicUserValidationService>();
199+
IBasicUserValidationService basicUserValidationService = null;
200+
if (Options.BasicUserValidationServiceType != null)
201+
{
202+
basicUserValidationService = ActivatorUtilities.GetServiceOrCreateInstance(Context.RequestServices, Options.BasicUserValidationServiceType) as IBasicUserValidationService;
203+
}
204+
200205
if (basicUserValidationService == null)
201206
{
202-
if (Options.BasicUserValidationServiceType != null)
203-
{
204-
basicUserValidationService = Context.RequestServices.GetService(Options.BasicUserValidationServiceType) as IBasicUserValidationService;
205-
}
207+
throw new InvalidOperationException($"Either {nameof(Options.Events.OnValidateCredentials)} delegate on configure options {nameof(Options.Events)} should be set or use an extention method with type parameter of type {nameof(IBasicUserValidationService)}.");
208+
}
206209

207-
if (basicUserValidationService == null)
210+
try
211+
{
212+
return await basicUserValidationService.IsValidAsync(username, password).ConfigureAwait(false);
213+
}
214+
finally
215+
{
216+
if (basicUserValidationService is IDisposable disposableBasicUserValidationService)
208217
{
209-
throw new InvalidOperationException($"Either {nameof(Options.Events.OnValidateCredentials)} delegate on configure options {nameof(Options.Events)} should be set or an implementaion of {nameof(IBasicUserValidationService)} should be registered in the dependency container.");
218+
disposableBasicUserValidationService.Dispose();
210219
}
211220
}
212-
213-
return await basicUserValidationService.IsValidAsync(username, password).ConfigureAwait(false);
214221
}
215222

216223
private BasicCredentials DecodeBasicCredentials(string credentials)

src/AspNetCore.Authentication.Basic/BasicPostConfigureOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void PostConfigure(string name, BasicOptions options)
2020

2121
if (options.Events?.OnValidateCredentials == null && options.EventsType == null && options.BasicUserValidationServiceType == null)
2222
{
23-
throw new InvalidOperationException($"Either {nameof(options.Events.OnValidateCredentials)} delegate on configure options {nameof(options.Events)} should be set or an implementaion of {nameof(IBasicUserValidationService)} should be registered in the dependency container.");
23+
throw new InvalidOperationException($"Either {nameof(BasicOptions.Events.OnValidateCredentials)} delegate on configure options {nameof(BasicOptions.Events)} should be set or use an extention method with type parameter of type {nameof(IBasicUserValidationService)}.");
2424
}
2525
}
2626
}

0 commit comments

Comments
 (0)