File tree Expand file tree Collapse file tree
BasicAuthenticationSample
src/SimpleAuthentication.Abstractions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using System . Security . Claims ;
22using ApiKeySample . Authentication ;
33using Microsoft . AspNetCore . Authentication ;
4+ using Microsoft . Extensions . Options ;
45using SimpleAuthentication ;
56using SimpleAuthentication . ApiKey ;
67
5556app . UseAuthentication ( ) ;
5657app . UseAuthorization ( ) ;
5758
58- app . MapGet ( "api/me" , ( ClaimsPrincipal user ) =>
59+ app . MapGet ( "api/me" , ( ClaimsPrincipal user , IOptions < ApiKeySettings > options ) =>
5960{
60- var roles = user . FindAll ( ClaimTypes . Role ) . Select ( c => c . Value ) ;
61+ var roles = user . FindAll ( options . Value . RoleClaimType ) . Select ( c => c . Value ) ;
6162 return TypedResults . Ok ( new User ( user . Identity ! . Name , roles ) ) ;
6263} )
6364. RequireAuthorization ( )
6465. WithOpenApi ( ) ;
6566
66- app . MapGet ( "api/admin" , ( ) => "Admin access granted" )
67- . RequireAuthorization ( policy => policy . RequireRole ( "Admin " ) )
67+ app . MapGet ( "api/admin" , ( ) => "Administrator access granted" )
68+ . RequireAuthorization ( policy => policy . RequireRole ( "Administrator " ) )
6869. WithOpenApi ( ) ;
6970
7071app . MapGet ( "api/user" , ( ) => "User access granted" )
Original file line number Diff line number Diff line change 1111 // You can set a fixed API Key for authentication. If you have a single value, you can just use the plain property:
1212 "ApiKeyValue" : " f1I7S5GXa4wQDgLQWgz0" ,
1313 "UserName" : " ApiUser" , // Required if ApiKeyValue is used
14+ "Roles" : [ " Administrator" ],
1415 // Otherwise, you can create an array of ApiKeys:
1516 "ApiKeys" : [
1617 {
1718 "Value" : " ArAilHVOoL3upX78Cohq" ,
1819 "UserName" : " alice" ,
19- "Roles" : [ " Admin " , " User" ]
20+ "Roles" : [ " Administrator " , " User" ]
2021 },
2122 {
2223 "Value" : " DiUU5EqImTYkxPDAxBVS" ,
Original file line number Diff line number Diff line change 11using System . Security . Claims ;
22using BasicAuthenticationSample . Authentication ;
33using Microsoft . AspNetCore . Authentication ;
4+ using Microsoft . Extensions . Options ;
45using SimpleAuthentication ;
56using SimpleAuthentication . BasicAuthentication ;
67
5556app . UseAuthentication ( ) ;
5657app . UseAuthorization ( ) ;
5758
58- app . MapGet ( "api/me" , ( ClaimsPrincipal user ) =>
59+ app . MapGet ( "api/me" , ( ClaimsPrincipal user , IOptions < BasicAuthenticationSettings > options ) =>
5960{
60- var roles = user . FindAll ( ClaimTypes . Role ) . Select ( c => c . Value ) ;
61+ var roles = user . FindAll ( options . Value . RoleClaimType ) . Select ( c => c . Value ) ;
6162 return TypedResults . Ok ( new User ( user . Identity ! . Name , roles ) ) ;
6263} )
6364. RequireAuthorization ( )
6465. WithOpenApi ( ) ;
6566
66- app . MapGet ( "api/admin" , ( ) => "Admin access granted" )
67- . RequireAuthorization ( policy => policy . RequireRole ( "Admin " ) )
67+ app . MapGet ( "api/admin" , ( ) => "Administrator access granted" )
68+ . RequireAuthorization ( policy => policy . RequireRole ( "Administrator " ) )
6869. WithOpenApi ( ) ;
6970
7071app . MapGet ( "api/user" , ( ) => "User access granted" )
Original file line number Diff line number Diff line change 88 //"RoleClaimType": "user_role", // Default: http://schemas.microsoft.com/ws/2008/06/identity/claims/role
99 "UserName" : " marco" ,
1010 "Password" : " P@$$w0rd" ,
11+ "Roles" : [ " Administrator" ],
1112 // Otherwise, you can create an array of Credentials:
1213 "Credentials" : [
1314 {
1415 "UserName" : " alice" ,
1516 "Password" : " Password1" ,
16- "Roles" : [ " Admin " , " User" ]
17+ "Roles" : [ " Administrator " , " User" ]
1718 },
1819 {
1920 "UserName" : " bob" ,
Original file line number Diff line number Diff line change @@ -41,6 +41,13 @@ public class ApiKeySettings : AuthenticationSchemeOptions
4141 /// <seealso cref="ApiKeyValue"/>
4242 public string ? UserName { get ; set ; }
4343
44+ /// <summary>
45+ /// Gets or sets the optional list of roles to assign to the user when using <see cref="ApiKeyValue"/> and <see cref="UserName"/>.
46+ /// </summary>
47+ /// <seealso cref="ApiKeyValue"/>
48+ /// <seealso cref="UserName"/>
49+ public string [ ] ? Roles { get ; set ; }
50+
4451 private ICollection < ApiKey > apiKeys = [ ] ;
4552 /// <summary>
4653 /// The collection of valid API keys.
@@ -53,7 +60,7 @@ public ICollection<ApiKey> ApiKeys
5360 if ( ! string . IsNullOrWhiteSpace ( ApiKeyValue ) && ! string . IsNullOrWhiteSpace ( UserName ) )
5461 {
5562 // If necessary, add the API Key from the base properties.
56- apiKeys . Add ( new ( ApiKeyValue , UserName ) ) ;
63+ apiKeys . Add ( new ( ApiKeyValue , UserName , Roles ) ) ;
5764 }
5865
5966 return apiKeys ;
Original file line number Diff line number Diff line change @@ -30,6 +30,13 @@ public class BasicAuthenticationSettings : AuthenticationSchemeOptions
3030 /// <seealso cref="IBasicAuthenticationValidator"/>
3131 public string ? Password { get ; set ; }
3232
33+ /// <summary>
34+ /// Gets or sets the optional list of roles to assign to the user when using <see cref="UserName"/> and <see cref="Password"/>.
35+ /// </summary>
36+ /// <seealso cref="UserName"/>
37+ /// <seealso cref="Password"/>
38+ public string [ ] ? Roles { get ; set ; }
39+
3340 private ICollection < Credential > credentials = [ ] ;
3441 /// <summary>
3542 /// The collection of authorization credentials.
@@ -42,7 +49,7 @@ public ICollection<Credential> Credentials
4249 if ( ! string . IsNullOrWhiteSpace ( UserName ) && ! string . IsNullOrWhiteSpace ( Password ) )
4350 {
4451 // If necessary, add the credentials from the base properties.
45- credentials . Add ( new Credential ( UserName , Password ) ) ;
52+ credentials . Add ( new Credential ( UserName , Password , Roles ) ) ;
4653 }
4754
4855 return credentials ;
You can’t perform that action at this time.
0 commit comments