Skip to content

Commit

Permalink
- Fixed issue with resolving of IBasicUserValidationService implement…
Browse files Browse the repository at this point in the history
…ation when using multiple schemes

- Updated version
- Updated samples
  • Loading branch information
mihirdilip committed Oct 31, 2020
1 parent 30dbf69 commit a877f5d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 60 deletions.
13 changes: 2 additions & 11 deletions samples/SampleWebApi_2_0/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3920/",
"sslPort": 0
"applicationUrl": "http://localhost:3920",
"sslPort": 44304
}
},
"profiles": {
Expand All @@ -15,15 +15,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleWebApi": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:3921/"
}
}
}
4 changes: 2 additions & 2 deletions samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
Expand All @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
</ItemGroup>

Expand Down
13 changes: 2 additions & 11 deletions samples/SampleWebApi_2_2/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1938",
"applicationUrl": "http://localhost:3920",
"sslPort": 44304
}
},
Expand All @@ -16,15 +16,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleWebApi_2_2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
2 changes: 1 addition & 1 deletion samples/SampleWebApi_2_2/SampleWebApi_2_2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
Expand Down
13 changes: 2 additions & 11 deletions samples/SampleWebApi_3_1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:2756",
"sslPort": 44301
"applicationUrl": "http://localhost:3920",
"sslPort": 44304
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
Expand All @@ -16,15 +16,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleWebApi_3_1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
2 changes: 1 addition & 1 deletion samples/SampleWebApi_3_1/SampleWebApi_3_1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />

<ItemGroup>
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.1" />
</ItemGroup>

<!--<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp3.0;netstandard2.0;net461</TargetFrameworks>
<Version>3.1.0</Version>
<RepositoryUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/3.1.0</RepositoryUrl>
<PackageProjectUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/3.1.0</PackageProjectUrl>
<Version>3.1.1</Version>
<RepositoryUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</RepositoryUrl>
<PackageProjectUrl>https://github.com/mihirdilip/aspnetcore-authentication-basic/tree/$(Version)</PackageProjectUrl>
<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>
<PackageReleaseNotes>- Multitarget framework support added
- Source Link support added
- Strong Name Key support added
- SuppressWWWAuthenticateHeader added to configure options
- Events added to configure options
<PackageReleaseNotes>- Fixed issue with resolving of IBasicUserValidationService implementation when using multiple schemes
</PackageReleaseNotes>
<Description>Easy to use and very light weight Microsoft style Basic Scheme Authentication implementation for ASP.NET Core.</Description>
<Authors>Mihir Dilip</Authors>
Expand Down
10 changes: 5 additions & 5 deletions src/AspNetCore.Authentication.Basic/BasicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class BasicExtensions
{
/// <summary>
/// Adds basic authentication scheme to the project.
/// <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.
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/>.
/// </summary>
/// <param name="builder"></param>
/// <returns>The instance of <see cref="AuthenticationBuilder"/></returns>
Expand All @@ -25,7 +25,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder)

/// <summary>
/// Adds basic authentication scheme to the project.
/// <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.
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the <see cref="BasicOptions.Events"/>.
/// </summary>
/// <param name="builder"></param>
/// <param name="authenticationScheme">The authentication scheme.</param>
Expand All @@ -35,7 +35,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,

/// <summary>
/// Adds basic authentication scheme to the project.
/// <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.
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
/// </summary>
/// <param name="builder"></param>
/// <param name="configureOptions">The configure options.</param>
Expand All @@ -45,7 +45,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,

/// <summary>
/// Adds basic authentication scheme to the project.
/// <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.
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
/// </summary>
/// <param name="builder"></param>
/// <param name="authenticationScheme">The authentication scheme.</param>
Expand All @@ -56,7 +56,7 @@ public static AuthenticationBuilder AddBasic(this AuthenticationBuilder builder,

/// <summary>
/// Adds basic authentication scheme to the project.
/// <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.
/// <see cref="BasicEvents.OnValidateCredentials"/> delegate must be set on the Events property on <paramref name="configureOptions"/>.
/// </summary>
/// <param name="builder"></param>
/// <param name="authenticationScheme">The authentication scheme.</param>
Expand Down
25 changes: 16 additions & 9 deletions src/AspNetCore.Authentication.Basic/BasicHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,28 @@ private async Task<AuthenticateResult> RaiseAndHandleAuthenticationSucceededAsyn

private async Task<bool> ValidateUsingBasicUserValidationServiceAsync(string username, string password)
{
var basicUserValidationService = Context.RequestServices.GetService<IBasicUserValidationService>();
IBasicUserValidationService basicUserValidationService = null;
if (Options.BasicUserValidationServiceType != null)
{
basicUserValidationService = ActivatorUtilities.GetServiceOrCreateInstance(Context.RequestServices, Options.BasicUserValidationServiceType) as IBasicUserValidationService;
}

if (basicUserValidationService == null)
{
if (Options.BasicUserValidationServiceType != null)
{
basicUserValidationService = Context.RequestServices.GetService(Options.BasicUserValidationServiceType) as IBasicUserValidationService;
}
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)}.");
}

if (basicUserValidationService == null)
try
{
return await basicUserValidationService.IsValidAsync(username, password).ConfigureAwait(false);
}
finally
{
if (basicUserValidationService is IDisposable disposableBasicUserValidationService)
{
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.");
disposableBasicUserValidationService.Dispose();
}
}

return await basicUserValidationService.IsValidAsync(username, password).ConfigureAwait(false);
}

private BasicCredentials DecodeBasicCredentials(string credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void PostConfigure(string name, BasicOptions options)

if (options.Events?.OnValidateCredentials == null && options.EventsType == null && options.BasicUserValidationServiceType == null)
{
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.");
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)}.");
}
}
}
Expand Down

0 comments on commit a877f5d

Please sign in to comment.