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
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.
22
22
23
23
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.
24
24
25
25
Also add an implementation of *IBasicUserValidationService* as shown below in [**BasicUserValidationService.cs**](#basicuservalidationservicecs).
26
26
27
27
**NOTE: Always use HTTPS (SSL Certificate) protocol in production when using Basic authentication.**
@@ -86,7 +142,36 @@ public class BasicUserValidationService : IBasicUserValidationService
86
142
}
87
143
```
88
144
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.
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("/", asynccontext=>
170
+
{
171
+
awaitcontext.Response.WriteAsync("Hello World!");
172
+
}).RequireAuthorization(); // NOTE THIS HERE!!!!
173
+
});
174
+
```
90
175
91
176
## References
92
177
-[Creating an authentication scheme in ASP.NET Core 2.0](https://joonasw.net/view/creating-auth-scheme-in-aspnet-core-2)
0 commit comments