File tree Expand file tree Collapse file tree 4 files changed +22
-5
lines changed
Scenarios/Authentication/Minimal-machine-to-machine-using-JWT Expand file tree Collapse file tree 4 files changed +22
-5
lines changed Original file line number Diff line number Diff line change 1- using System . Net ;
1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using System . Net ;
25using System . ServiceModel ;
36using System . ServiceModel . Channels ;
47using IdentityModel . Client ;
Original file line number Diff line number Diff line change 1- namespace Service ;
1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ namespace Service ;
25
36[ ServiceContract ]
47public interface ISecuredService
Original file line number Diff line number Diff line change 1- using Microsoft . AspNetCore . Authentication . JwtBearer ;
1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
25using Microsoft . AspNetCore . Authorization ;
36
47var builder = WebApplication . CreateBuilder ( ) ;
1316{
1417 options . DefaultPolicy = new AuthorizationPolicyBuilder ( JwtBearerDefaults . AuthenticationScheme )
1518 . RequireAuthenticatedUser ( )
16- . RequireClaim ( "scope" , "api" )
19+ . RequireAssertion ( context =>
20+ {
21+ string [ ] scopes = context . User . FindFirst ( "scope" ) ? . Value . Split ( ' ' , StringSplitOptions . RemoveEmptyEntries )
22+ ?? Array . Empty < string > ( ) ;
23+ return scopes . Any ( x => string . Equals ( x , "api" , StringComparison . Ordinal ) ) ;
24+ } )
1725 . Build ( ) ;
1826} ) ;
1927builder . Services . AddTransient < SecuredService > ( ) ;
Original file line number Diff line number Diff line change 1- using Microsoft . AspNetCore . Authorization ;
1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using Microsoft . AspNetCore . Authorization ;
25using Microsoft . AspNetCore . Mvc ;
36
47namespace Service
You can’t perform that action at this time.
0 commit comments