File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
aspnetcore/security/authentication Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -210,6 +210,28 @@ services.AddAuthentication(options =>
210210 };
211211});
212212```
213+ ### Forcing the bearer authentication
214+
215+ The ** SetFallbackPolicy** can be used to require an authentication if no policy is defined.
216+
217+ ``` csharp
218+ var requireAuthPolicy = new AuthorizationPolicyBuilder ()
219+ .RequireAuthenticatedUser ()
220+ .Build ();
221+
222+ builder .Services .AddAuthorizationBuilder ()
223+ .SetFallbackPolicy (requireAuthPolicy );
224+ ```
225+
226+ The ** Authorize** attribute can alos be used to force the authentication. If multiple schemes are used, the bearer scheme needs to be set.
227+
228+ ``` csharp
229+ [Authorize ]
230+ [ApiController ]
231+ [Route (" [controller]" )]
232+ public class WeatherForecastController : ControllerBase
233+ {
234+ ```
213235
214236## Recommended approaches to create a JWT
215237
You can’t perform that action at this time.
0 commit comments