You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Multi targeted: net5.0; netcoreapp3.1; netcoreapp3.0; netstandard2.0; net461
11
+
12
+
<br/>
13
+
6
14
## Installing
7
15
This library is published on NuGet. So the NuGet package can be installed directly to your project if you wish to use it without making any custom changes to the code.
8
16
@@ -16,37 +24,41 @@ Or by running the below command on your project.
Samples are available under [samples directory](samples).
22
32
23
-
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.
33
+
Setting it up is quite simple. You will need basic working knowledge of ASP.NET Core 2.0 or newer to get started using this library.
24
34
25
-
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.
35
+
There are 2 different ways of using this library to do it's job. Both ways can be mixed if required.
36
+
1] Using the implementation of *IBasicUserValidationService*
37
+
2] Using *BasicOptions.Events* (OnValidateCredentials delegate) which is same approach you will find on Microsoft's authentication libraries
26
38
27
-
Also add an implementation of *IBasicUserValidationService* as shown below in [**BasicUserValidationService.cs**](#basicuservalidationservicecs).
39
+
Notes:
40
+
- It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
41
+
- If an implementation of IBasicUserValidationService interface is used as well as BasicOptions.Events.OnValidateCredentials delegate is also set then this delegate will be used first.
28
42
29
-
**NOTE: Always use HTTPS (SSL Certificate) protocol in production when using Basic authentication.**
43
+
**Always use HTTPS (SSL Certificate) protocol in production when using API Key authentication.**
// 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.
54
+
// If an implementation of IBasicUserValidationService interface is used as well as options.Events.OnValidateCredentials delegate is also set then this delegate will be used first.
// 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.
58
+
// The below AddBasic without type parameter will require options.Events.OnValidateCredentials delegete to be set.
46
59
//.AddBasic(options => { options.Realm = "My App"; });
47
60
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.
61
+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency container.
// 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.
101
+
// If an implementation of IBasicUserValidationService interface is used as well as options.Events.OnValidateCredentials delegate is also set then this delegate will be used first.
// 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.
105
+
// The below AddBasic without type parameter will require options.Events.OnValidateCredentials delegete to be set.
95
106
//.AddBasic(options => { options.Realm = "My App"; });
96
107
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.
108
+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency container.
@@ -144,60 +154,70 @@ public class BasicUserValidationService : IBasicUserValidationService
144
154
}
145
155
```
146
156
157
+
<br/>
158
+
<br/>
159
+
147
160
## Configuration (BasicOptions)
148
-
#### Realm
161
+
162
+
### Realm
149
163
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
164
151
-
####SuppressWWWAuthenticateHeader
165
+
### SuppressWWWAuthenticateHeader
152
166
Default value is false.
153
167
When set to true, it will NOT return WWW-Authenticate response header when challenging un-authenticated requests.
154
168
When set to false, it will return WWW-Authenticate response header when challenging un-authenticated requests.
155
169
156
-
####IgnoreAuthenticationIfAllowAnonymous
170
+
### IgnoreAuthenticationIfAllowAnonymous (available on ASP.NET Core 3.0 onwards)
157
171
Default value is false.
158
172
If set to true, it checks if AllowAnonymous filter on controller action or metadata on the endpoint which, if found, it does not try to authenticate the request.
159
173
160
-
####Events
174
+
### Events
161
175
The object provided by the application to process events raised by the basic authentication middleware.
162
176
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.
163
-
-#####OnValidateCredentials
177
+
-#### OnValidateCredentials
164
178
A delegate assigned to this property will be invoked just before validating credentials.
165
179
You must provide a delegate for this property for authentication to occur.
166
180
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.
167
181
If only context.Principal property set without calling context.Success() method then, Success() method is automaticalled called.
168
182
169
-
-#####OnAuthenticationSucceeded
183
+
-#### OnAuthenticationSucceeded
170
184
A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateCredentials delegate is assigned.
171
185
It can be used for adding claims, headers, etc to the response.
172
186
173
-
-#####OnAuthenticationFailed
187
+
-#### OnAuthenticationFailed
174
188
A delegate assigned to this property will be invoked when the authentication fails.
175
189
176
-
-#####OnHandleChallenge
190
+
-#### OnHandleChallenge
177
191
A delegate assigned to this property will be invoked before a challenge is sent back to the caller when handling unauthorized response.
178
192
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.)
179
193
Call context.Handled() at the end so that any default logic for this challenge will be skipped.
180
194
181
-
-#####OnHandleForbidden
195
+
-#### OnHandleForbidden
182
196
A delegate assigned to this property will be invoked if Authorization fails and results in a Forbidden response.
183
197
Only use this if you know what you are doing and if you want to use custom implementation.
184
198
Set the delegate to handle Forbid.
185
199
Call context.Handled() at the end so that any default logic will be skipped.
186
200
201
+
<br/>
202
+
<br/>
187
203
188
204
## Additional Notes
189
-
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.
205
+
206
+
### Basic Authentication Not Challenged
207
+
With ASP.NET Core, all the requests are not challenged for authentication by default. 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.
190
208
191
209
However, if you want all the requests to challenge authentication by default, depending on what you are using, you can add the below options line to *ConfigureServices* method on *Startup* class.
If you are not using MVC but, using Endpoints on ASP.NET Core 3.0 or newer, you can add a chain method `.RequireAuthorization()` to the endpoint map under *Configure* method on *Startup* class as shown below.
ASP.NET Core supports adding multiple authentication schemes which this library also supports. Just need to use the extension method which takes scheme name as parameter. The rest is all same. This can be achieved in many different ways. Below is just a quick rough example.
242
+
243
+
Please note that scheme name parameter can be any string you want.
| Version | Notes |
297
+
|---------|-------|
298
+
|5.1.0 | <ul><li>Visibility of all the handlers changed to public</li><li>Tests added</li><li>Readme updated</li><li>Copyright year updated on License</li></ul> |
299
+
|5.0.0 | <ul><li>Net 5.0 target framework added</li><li>IgnoreAuthenticationIfAllowAnonymous added to the ApiKeyOptions from netcoreapp3.0 onwards [#15](https://github.com/mihirdilip/aspnetcore-authentication-apikey/issues/15)</li></ul> |
300
+
|3.1.1 | <ul><li>Fixed issue with resolving of IBasicUserValidationService implementation when using multiple schemes</li></ul> |
301
+
|3.1.0 | <ul><li>Multitarget framework support added</li><li>Strong Name Key support added</li><li>Source Link support added</li><li>SuppressWWWAuthenticateHeader added to configure options</li><li>Events added to configure options</li></ul> |
302
+
|2.2.0 | <ul><li>Basic Authentication Implementation for ASP.NET Core</li></ul> |
303
+
304
+
<br/>
305
+
<br/>
306
+
219
307
## References
220
308
-[RFC 7617: Technical spec for HTTP Basic](https://tools.ietf.org/html/rfc7617)
0 commit comments