Skip to content

Commit 443b5af

Browse files
committed
Readme and License updated
1 parent d5dce0a commit 443b5af

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Mihir Dilip
3+
Copyright (c) 2020 Mihir Dilip
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,64 @@ PM> Install-Package AspNetCore.Authentication.Basic
1818

1919
## Example Usage
2020

21-
Setting it up is quite simple. You will need basic working knowledge of ASP.NET Core 2.2 to get started using this code.
21+
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.
2222

2323
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.
2424

2525
Also add an implementation of *IBasicUserValidationService* as shown below in [**BasicUserValidationService.cs**](#basicuservalidationservicecs).
2626

2727
**NOTE: Always use HTTPS (SSL Certificate) protocol in production when using Basic authentication.**
2828

29-
#### Startup.cs
29+
#### Startup.cs (ASP.NET Core 3.0 or newer)
30+
31+
```C#
32+
using AspNetCore.Authentication.Basic;
33+
public class Startup
34+
{
35+
public Startup(IConfiguration configuration)
36+
{
37+
Configuration = configuration;
38+
}
39+
40+
public IConfiguration Configuration { get; }
41+
42+
public void ConfigureServices(IServiceCollection services)
43+
{
44+
// 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"; });
49+
50+
services.AddControllers();
51+
52+
//// By default, authentication is not challenged for every request which is ASP.NET Core's default intended behaviour.
53+
//// So to challenge authentication for every requests please use below option instead of above services.AddControllers().
54+
//services.AddControllers(options =>
55+
//{
56+
// options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
57+
//});
58+
}
59+
60+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
61+
{
62+
app.UseHttpsRedirection();
63+
64+
// The below order of pipeline chain is important!
65+
app.UseRouting();
66+
67+
app.UseAuthentication();
68+
app.UseAuthorization();
69+
70+
app.UseEndpoints(endpoints =>
71+
{
72+
endpoints.MapControllers();
73+
});
74+
}
75+
}
76+
```
77+
78+
#### Startup.cs (ASP.NET Core 2.2)
3079

3180
```C#
3281
using AspNetCore.Authentication.Basic;
@@ -48,6 +97,13 @@ public class Startup
4897
.AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });
4998

5099
services.AddMvc();
100+
101+
//// By default, authentication is not challenged for every request which is ASP.NET Core's default intended behaviour.
102+
//// So to challenge authentication for every requests please use below option instead of above services.AddMvc().
103+
//services.AddMvc(options =>
104+
//{
105+
// options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
106+
//});
51107
}
52108

53109
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -86,7 +142,36 @@ public class BasicUserValidationService : IBasicUserValidationService
86142
}
87143
```
88144

89-
145+
## Additional Notes
146+
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.
147+
148+
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.
149+
150+
```C#
151+
services.AddControllers(options =>
152+
{
153+
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
154+
});
155+
156+
// OR
157+
158+
services.AddMvc(options =>
159+
{
160+
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
161+
});
162+
```
163+
164+
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.
165+
166+
```C#
167+
app.UseEndpoints(endpoints =>
168+
{
169+
endpoints.MapGet("/", async context =>
170+
{
171+
await context.Response.WriteAsync("Hello World!");
172+
}).RequireAuthorization(); // NOTE THIS HERE!!!!
173+
});
174+
```
90175

91176
## References
92177
- [Creating an authentication scheme in ASP.NET Core 2.0](https://joonasw.net/view/creating-auth-scheme-in-aspnet-core-2)

src/AspNetCore.Authentication.Basic.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_2_2", "..\samp
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Authentication.Basic", "AspNetCore.Authentication.Basic\AspNetCore.Authentication.Basic.csproj", "{B2492C13-5F4E-4002-9E66-0ACF73045668}"
1515
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7BFEA4E3-5506-40A1-B6EB-BC8BA3687D23}"
17+
ProjectSection(SolutionItems) = preProject
18+
..\LICENSE.txt = ..\LICENSE.txt
19+
..\README.md = ..\README.md
20+
EndProjectSection
21+
EndProject
1622
Global
1723
GlobalSection(SharedMSBuildProjectFiles) = preSolution
24+
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{07c59e2a-3e40-4fc0-a8cf-0656ab00e8a7}*SharedItemsImports = 5
25+
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{0801aed9-ea38-4e7e-af4d-26e9b67e5254}*SharedItemsImports = 5
1826
..\samples\SampleWebApi.Shared\SampleWebApi.Shared.projitems*{e544fb20-29f3-41f5-a78e-6164f9c43b3c}*SharedItemsImports = 13
1927
EndGlobalSection
2028
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)