Skip to content

Commit 1f7daa2

Browse files
Copilotmarcominerva
andcommitted
Update sample applications to test roles feature and fix project references
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
1 parent 6442cd6 commit 1f7daa2

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

samples/MinimalApis/ApiKeySample/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@
5757

5858
app.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+
6574
app.Run();
6675

67-
public record class User(string? UserName);
76+
public record class User(string? UserName, IEnumerable<string> Roles);
6877

6978
public class CustomApiKeyValidator : IApiKeyValidator
7079
{

samples/MinimalApis/ApiKeySample/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
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.

samples/MinimalApis/BasicAuthenticationSample/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@
5757

5858
app.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+
6574
app.Run();
6675

67-
public record class User(string? UserName);
76+
public record class User(string? UserName, IEnumerable<string> Roles);
6877

6978
public class CustomBasicAuthenticationValidator : IBasicAuthenticationValidator
7079
{

samples/MinimalApis/BasicAuthenticationSample/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
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.

src/SimpleAuthentication.Swashbuckle/SimpleAuthentication.Swashbuckle.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
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

0 commit comments

Comments
 (0)