Skip to content

Commit 2cf3940

Browse files
committed
Change the way how authnetication is handled. Now by assigning Principal to context. Allowing events to be used by dependency injection #4.
1 parent 2eb25fe commit 2cf3940

File tree

11 files changed

+243
-92
lines changed

11 files changed

+243
-92
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ Install using the [ZNetCS.AspNetCore.Authentication.Basic NuGet package](https:/
1212
PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic
1313
```
1414

15+
# Important change in 3.0.0
16+
The `OnValidatePrincipal` will not return `AuthenticationResult` any more. To simplify process can simply return `Task.CompletedTask`.
17+
Also to make success authentication `Principal` have to be assigned to `ValidatePrincipalContext` context.
18+
1519
## Usage
1620

1721
When you install the package, it should be added to your `.csproj`. Alternatively, you can add it directly by adding:
1822

1923

2024
```xml
2125
<ItemGroup>
22-
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="2.0.0" />
26+
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="3.0.0" />
2327
</ItemGroup>
2428
```
2529

@@ -64,15 +68,14 @@ public void ConfigureServices(IServiceCollection services)
6468
new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
6569
};
6670

67-
var ticket = new AuthenticationTicket(
68-
new ClaimsPrincipal(new ClaimsIdentity(claims, BasicAuthenticationDefaults.AuthenticationScheme)),
69-
new Microsoft.AspNetCore.Authentication.AuthenticationProperties(),
70-
BasicAuthenticationDefaults.AuthenticationScheme);
71+
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, BasicAuthenticationDefaults.AuthenticationScheme));
72+
context.Principal = principal;
7173

72-
return Task.FromResult(AuthenticateResult.Success(ticket));
74+
// optional with following default.
75+
// context.AuthenticationFailMessage = "Authentication failed.";
7376
}
7477

75-
return Task.FromResult(AuthenticateResult.Fail("Authentication failed."));
78+
return Task.CompletedTask;
7679
}
7780
};
7881
});

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationExtensions.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public static class BasicAuthenticationExtensions
3131
/// The authentication builder.
3232
/// </param>
3333
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder)
34-
{
35-
return builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme);
36-
}
34+
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme);
3735

3836
/// <summary>
3937
/// Adds basic authentication.
@@ -45,9 +43,7 @@ public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBu
4543
/// The authentication scheme.
4644
/// </param>
4745
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, string authenticationScheme)
48-
{
49-
return builder.AddBasicAuthentication(authenticationScheme, null);
50-
}
46+
=> builder.AddBasicAuthentication(authenticationScheme, null);
5147

5248
/// <summary>
5349
/// Adds basic authentication.
@@ -59,9 +55,7 @@ public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBu
5955
/// The configure options.
6056
/// </param>
6157
public static AuthenticationBuilder AddBasicAuthentication(this AuthenticationBuilder builder, Action<BasicAuthenticationOptions> configureOptions)
62-
{
63-
return builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);
64-
}
58+
=> builder.AddBasicAuthentication(BasicAuthenticationDefaults.AuthenticationScheme, configureOptions);
6559

6660
/// <summary>
6761
/// Adds basic authentication.
@@ -79,9 +73,7 @@ public static AuthenticationBuilder AddBasicAuthentication(
7973
this AuthenticationBuilder builder,
8074
string authenticationScheme,
8175
Action<BasicAuthenticationOptions> configureOptions)
82-
{
83-
return builder.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(authenticationScheme, configureOptions);
84-
}
76+
=> builder.AddScheme<BasicAuthenticationOptions, BasicAuthenticationHandler>(authenticationScheme, configureOptions);
8577

8678
#endregion
8779
}

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationHandler.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,26 @@ public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> op
7878

7979
#endregion
8080

81+
#region Properties
82+
83+
/// <summary>
84+
/// Gets or sets the events. The handler calls methods on the events which give the application control
85+
/// at certain points where processing is occurring. If it is not provided a default instance
86+
/// is supplied which does nothing when the methods are called.
87+
/// </summary>
88+
protected new BasicAuthenticationEvents Events
89+
{
90+
get => (BasicAuthenticationEvents)base.Events;
91+
set => base.Events = value;
92+
}
93+
94+
#endregion
95+
8196
#region Methods
8297

98+
/// <inheritdoc/>
99+
protected override Task<object> CreateEventsAsync() => Task.FromResult<object>(new BasicAuthenticationEvents());
100+
83101
/// <inheritdoc/>
84102
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
85103
{
@@ -131,7 +149,14 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
131149
string password = decodedCredentials.Substring(delimiterIndex + 1);
132150

133151
var context = new ValidatePrincipalContext(this.Context, this.Scheme, this.Options, userName, password);
134-
return await this.Options.Events.ValidatePrincipal(context);
152+
await this.Events.ValidatePrincipalAsync(context);
153+
154+
if (context.Principal == null)
155+
{
156+
return AuthenticateResult.Fail(context.AuthenticationFailMessage);
157+
}
158+
159+
return AuthenticateResult.Success(new AuthenticationTicket(context.Principal, context.Properties, this.Scheme.Name));
135160
}
136161

137162
/// <inheritdoc/>

src/ZNetCS.AspNetCore.Authentication.Basic/Events/BasicAuthenticationEvents.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ namespace ZNetCS.AspNetCore.Authentication.Basic.Events
1414
using System;
1515
using System.Threading.Tasks;
1616

17-
using Microsoft.AspNetCore.Authentication;
18-
1917
#endregion
2018

2119
/// <summary>
@@ -28,14 +26,11 @@ public class BasicAuthenticationEvents
2826
/// <summary>
2927
/// Gets or sets a delegate assigned to this property will be invoked when the related method is called.
3028
/// </summary>
31-
public Func<ValidatePrincipalContext, Task<AuthenticateResult>> OnValidatePrincipal { get; set; } =
32-
context => Task.FromResult(AuthenticateResult.Fail("Incorrect credentials."));
29+
public Func<ValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => Task.CompletedTask;
3330

3431
#endregion
3532

36-
#region Implemented Interfaces
37-
38-
#region IBasicAuthenticationEvents
33+
#region Public Methods
3934

4035
/// <summary>
4136
/// Called each time a request principal has been validated by the middleware. By implementing this method the
@@ -47,9 +42,7 @@ public class BasicAuthenticationEvents
4742
/// <returns>
4843
/// A <see cref="Task"/> representing the completed operation.
4944
/// </returns>
50-
public virtual Task<AuthenticateResult> ValidatePrincipal(ValidatePrincipalContext context) => this.OnValidatePrincipal(context);
51-
52-
#endregion
45+
public virtual Task ValidatePrincipalAsync(ValidatePrincipalContext context) => this.OnValidatePrincipal(context);
5346

5447
#endregion
5548
}

src/ZNetCS.AspNetCore.Authentication.Basic/Events/ValidatePrincipalContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public ValidatePrincipalContext(HttpContext context, AuthenticationScheme scheme
5555

5656
#region Public Properties
5757

58+
/// <summary>
59+
/// Gets or sets the authentication fail message.
60+
/// </summary>
61+
public string AuthenticationFailMessage { get; set; } = "Authentication failed.";
62+
5863
/// <summary>
5964
/// Gets the password.
6065
/// </summary>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageLicenseUrl>https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic/master/LICENSE</PackageLicenseUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<RepositoryUrl>https://github.com/msmolka/ZNetCS.AspNetCore.Authentication.Basic</RepositoryUrl>
16-
<VersionPrefix>2.0.0</VersionPrefix>
16+
<VersionPrefix>3.0.0</VersionPrefix>
1717
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1818
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1919
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
@@ -37,13 +37,13 @@
3737
</PropertyGroup>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.0" />
41-
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
40+
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.0.1" />
41+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
4242
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
4343
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
4444
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
45-
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.0.0" />
46-
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2">
45+
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.0.1" />
46+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
4747
<PrivateAssets>All</PrivateAssets>
4848
</PackageReference>
4949
</ItemGroup>

0 commit comments

Comments
 (0)