File tree Expand file tree Collapse file tree
BasicAuthenticationSample
src/SimpleAuthentication.Swashbuckle Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5757
5858app . MapGet ( "api/me" , ( ClaimsPrincipal user ) =>
5959{
60- return TypedResults . Ok ( new User ( user . Identity ! . Name ) ) ;
60+ var roles = user . FindAll ( ClaimTypes . Role ) . Select ( c => c . Value ) ;
61+ return TypedResults . Ok ( new User ( user . Identity ! . Name , roles ) ) ;
6162} )
6263. RequireAuthorization ( )
6364. WithOpenApi ( ) ;
6465
66+ app . MapGet ( "api/admin" , ( ) => "Admin access granted" )
67+ . RequireAuthorization ( policy => policy . RequireRole ( "Admin" ) )
68+ . WithOpenApi ( ) ;
69+
70+ app . MapGet ( "api/user" , ( ) => "User access granted" )
71+ . RequireAuthorization ( policy => policy . RequireRole ( "User" ) )
72+ . WithOpenApi ( ) ;
73+
6574app . Run ( ) ;
6675
67- public record class User ( string ? UserName ) ;
76+ public record class User ( string ? UserName , IEnumerable < string > Roles ) ;
6877
6978public class CustomApiKeyValidator : IApiKeyValidator
7079{
Original file line number Diff line number Diff line change 1515 "ApiKeys" : [
1616 {
1717 "Value" : " ArAilHVOoL3upX78Cohq" ,
18- "UserName" : " alice"
18+ "UserName" : " alice" ,
19+ "Roles" : [ " Admin" , " User" ]
1920 },
2021 {
2122 "Value" : " DiUU5EqImTYkxPDAxBVS" ,
22- "UserName" : " bob"
23+ "UserName" : " bob" ,
24+ "Roles" : [ " User" ]
2325 }
2426 ]
2527 // You can also combine both declarations.
Original file line number Diff line number Diff line change 5757
5858app . MapGet ( "api/me" , ( ClaimsPrincipal user ) =>
5959{
60- return TypedResults . Ok ( new User ( user . Identity ! . Name ) ) ;
60+ var roles = user . FindAll ( ClaimTypes . Role ) . Select ( c => c . Value ) ;
61+ return TypedResults . Ok ( new User ( user . Identity ! . Name , roles ) ) ;
6162} )
6263. RequireAuthorization ( )
6364. WithOpenApi ( ) ;
6465
66+ app . MapGet ( "api/admin" , ( ) => "Admin access granted" )
67+ . RequireAuthorization ( policy => policy . RequireRole ( "Admin" ) )
68+ . WithOpenApi ( ) ;
69+
70+ app . MapGet ( "api/user" , ( ) => "User access granted" )
71+ . RequireAuthorization ( policy => policy . RequireRole ( "User" ) )
72+ . WithOpenApi ( ) ;
73+
6574app . Run ( ) ;
6675
67- public record class User ( string ? UserName ) ;
76+ public record class User ( string ? UserName , IEnumerable < string > Roles ) ;
6877
6978public class CustomBasicAuthenticationValidator : IBasicAuthenticationValidator
7079{
Original file line number Diff line number Diff line change 1212 "Credentials" : [
1313 {
1414 "UserName" : " alice" ,
15- "Password" : " Password1"
15+ "Password" : " Password1" ,
16+ "Roles" : [ " Admin" , " User" ]
1617 },
1718 {
1819 "UserName" : " bob" ,
19- "Password" : " Password2"
20+ "Password" : " Password2" ,
21+ "Roles" : [ " User" ]
2022 }
2123 ]
2224 // You can also combine both declarations.
Original file line number Diff line number Diff line change 3232 </ItemGroup >
3333
3434 <ItemGroup >
35- <PackageReference Include =" SimpleAuthenticationTools. Abstractions" Version = " 3.0.12 " />
35+ <ProjectReference Include =" ..\SimpleAuthentication. Abstractions\SimpleAuthentication.Abstractions.csproj " />
3636 <PackageReference Include =" Swashbuckle.AspNetCore.SwaggerGen" Version =" 9.0.4" />
3737 </ItemGroup >
3838
You can’t perform that action at this time.
0 commit comments