Skip to content

Commit c229064

Browse files
committed
Use the auth
1 parent ef3270f commit c229064

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

aspnetcore/security/authentication/configure-jwt-bearer-authentication.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)