Skip to content

Commit 2ff149e

Browse files
authored
Merge pull request #2 from mihirdilip/3.1.0
3.1.0 - Multitarget framework support added - Source Link support added - Strong Name Key support added - SuppressWWWAuthenticateHeader added to configure options - Events added to configure options
2 parents fd1601b + 12531dd commit 2ff149e

34 files changed

+1581
-262
lines changed

README.md

+65-27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PM> Install-Package AspNetCore.Authentication.Basic
1818

1919
## Example Usage
2020

21+
Samples are available under [samples directory](samples).
22+
2123
Setting it up is quite simple. You will need basic working knowledge of ASP.NET Core 2.2 or newer to get started using this code.
2224

2325
On [**Startup.cs**](#startupcs), as shown below, add 2 lines in *ConfigureServices* method `services.AddAuthentication(BasicDefaults.AuthenticationScheme).AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });`. And a line `app.UseAuthentication();` in *Configure* method.
@@ -32,20 +34,20 @@ Also add an implementation of *IBasicUserValidationService* as shown below in [*
3234
using AspNetCore.Authentication.Basic;
3335
public class Startup
3436
{
35-
public Startup(IConfiguration configuration)
36-
{
37-
Configuration = configuration;
38-
}
39-
40-
public IConfiguration Configuration { get; }
41-
4237
public void ConfigureServices(IServiceCollection services)
4338
{
4439
// Add the Basic scheme authentication here..
45-
// AddBasic extension takes an implementation of IBasicUserValidationService for validating the username and password.
46-
// It also requires Realm to be set in the options.
47-
services.AddAuthentication(BasicDefaults.AuthenticationScheme)
48-
.AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });
40+
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
41+
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
42+
services.AddAuthentication(BasicDefaults.AuthenticationScheme)
43+
44+
// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
45+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
46+
//.AddBasic(options => { options.Realm = "My App"; });
47+
48+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
49+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
50+
.AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });
4951

5052
services.AddControllers();
5153

@@ -81,20 +83,20 @@ public class Startup
8183
using AspNetCore.Authentication.Basic;
8284
public class Startup
8385
{
84-
public Startup(IConfiguration configuration)
85-
{
86-
Configuration = configuration;
87-
}
88-
89-
public IConfiguration Configuration { get; }
90-
9186
public void ConfigureServices(IServiceCollection services)
9287
{
9388
// Add the Basic scheme authentication here..
94-
// AddBasic extension takes an implementation of IBasicUserValidationService for validating the username and password.
95-
// It also requires Realm to be set in the options.
96-
services.AddAuthentication(BasicDefaults.AuthenticationScheme)
97-
.AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });
89+
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
90+
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
91+
services.AddAuthentication(BasicDefaults.AuthenticationScheme)
92+
93+
// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
94+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
95+
//.AddBasic(options => { options.Realm = "My App"; });
96+
97+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
98+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
99+
.AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });
98100

99101
services.AddMvc();
100102

@@ -141,7 +143,44 @@ public class BasicUserValidationService : IBasicUserValidationService
141143
}
142144
}
143145
```
144-
146+
147+
## Configuration (BasicOptions)
148+
#### Realm
149+
Required to be set if SuppressWWWAuthenticateHeader is not set to true. It is used with WWW-Authenticate response header when challenging un-authenticated requests.
150+
151+
#### SuppressWWWAuthenticateHeader
152+
Default value is false.
153+
When set to true, it will NOT return WWW-Authenticate response header when challenging un-authenticated requests.
154+
When set to false, it will return WWW-Authenticate response header when challenging un-authenticated requests.
155+
156+
#### Events
157+
The object provided by the application to process events raised by the basic authentication middleware.
158+
The application may implement the interface fully, or it may create an instance of BasicEvents and assign delegates only to the events it wants to process.
159+
- ##### OnValidateCredentials
160+
A delegate assigned to this property will be invoked just before validating credentials.
161+
You must provide a delegate for this property for authentication to occur.
162+
In your delegate you should either call context.ValidationSucceeded() which will handle construction of authentication principal from the user details which will be assiged the context.Principal property and call context.Success(), or construct an authentication principal from the user details & attach it to the context.Principal property and finally call context.Success() method.
163+
If only context.Principal property set without calling context.Success() method then, Success() method is automaticalled called.
164+
165+
- ##### OnAuthenticationSucceeded
166+
A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateCredentials delegate is assigned.
167+
It can be used for adding claims, headers, etc to the response.
168+
169+
- ##### OnAuthenticationFailed
170+
A delegate assigned to this property will be invoked when the authentication fails.
171+
172+
- ##### OnHandleChallenge
173+
A delegate assigned to this property will be invoked before a challenge is sent back to the caller when handling unauthorized response.
174+
Only use this if you know what you are doing and if you want to use custom implementation. Set the delegate to deal with 401 challenge concerns, if an authentication scheme in question deals an authentication interaction as part of it's request flow. (like adding a response header, or changing the 401 result to 302 of a login page or external sign-in location.)
175+
Call context.Handled() at the end so that any default logic for this challenge will be skipped.
176+
177+
- ##### OnHandleForbidden
178+
A delegate assigned to this property will be invoked if Authorization fails and results in a Forbidden response.
179+
Only use this if you know what you are doing and if you want to use custom implementation.
180+
Set the delegate to handle Forbid.
181+
Call context.Handled() at the end so that any default logic will be skipped.
182+
183+
145184
## Additional Notes
146185
Please note that, by default, with ASP.NET Core, all the requests are not challenged for authentication. So don't worry if your *BasicUserValidationService* is not hit when you don't pass the required basic authentication details with the request. It is a normal behaviour. ASP.NET Core challenges authentication only when it is specifically told to do so either by decorating controller/method with *[Authorize]* filter attribute or by some other means.
147186

@@ -174,10 +213,9 @@ app.UseEndpoints(endpoints =>
174213
```
175214

176215
## References
177-
- [Creating an authentication scheme in ASP.NET Core 2.0](https://joonasw.net/view/creating-auth-scheme-in-aspnet-core-2)
178-
- [aspnet/Security](https://github.com/aspnet/Security)
179-
- [ASP.NET Core Security documentation](https://docs.microsoft.com/en-us/aspnet/core/security)
180216
- [RFC 7617: Technical spec for HTTP Basic](https://tools.ietf.org/html/rfc7617)
217+
- [ASP.NET Core Security documentation](https://docs.microsoft.com/en-us/aspnet/core/security)
218+
- [aspnet/Security](https://github.com/dotnet/aspnetcore/tree/master/src/Security)
181219

182220
## License
183-
[MIT License](https://github.com/mihirdilip/aspnetcore-authentication-basic/blob/master/LICENSE.txt)
221+
[MIT License](https://github.com/mihirdilip/aspnetcore-authentication-basic/blob/master/LICENSE.txt)

samples/SampleWebApi/Controllers/ValuesController.cs

-41
This file was deleted.

samples/SampleWebApi/Startup.cs

-64
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SampleWebApi_2_2.Controllers
6+
{
7+
[Route("api/[controller]")]
8+
public class ValuesController : ControllerBase
9+
{
10+
// GET api/values
11+
[HttpGet]
12+
public IEnumerable<string> Get()
13+
{
14+
return new string[] { "value1", "value2" };
15+
}
16+
17+
[HttpGet("claims")]
18+
public string Claims()
19+
{
20+
var sb = new StringBuilder();
21+
foreach (var claim in User.Claims)
22+
{
23+
sb.AppendLine($"{claim.Type}: {claim.Value}");
24+
}
25+
return sb.ToString();
26+
}
27+
28+
[HttpGet("forbid")]
29+
public new IActionResult Forbid()
30+
{
31+
return base.Forbid();
32+
}
33+
}
34+
}
File renamed without changes.

samples/SampleWebApi/SampleWebApi.csproj renamed to samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Folder Include="wwwroot\" />
8+
<Compile Remove="wwwroot\**" />
9+
<Content Remove="wwwroot\**" />
10+
<EmbeddedResource Remove="wwwroot\**" />
11+
<None Remove="wwwroot\**" />
912
</ItemGroup>
1013

1114
<ItemGroup>
12-
<PackageReference Include="AspNetCore.Authentication.Basic" Version="2.2.0" />
15+
<PackageReference Include="AspNetCore.Authentication.Basic" Version="3.1.0" />
1316
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
1417
</ItemGroup>
1518

@@ -18,5 +21,9 @@
1821
</ItemGroup>
1922

2023
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
24+
25+
<!--<ItemGroup>
26+
<ProjectReference Include="..\..\src\AspNetCore.Authentication.Basic\AspNetCore.Authentication.Basic.csproj" />
27+
</ItemGroup>-->
2128

2229
</Project>

0 commit comments

Comments
 (0)