-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (55 loc) · 2.51 KB
/
Program.cs
File metadata and controls
74 lines (55 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Security.Claims;
using Auth0.ManagementApi;
using Auth0Net.DependencyInjection;
using Auth0Net.DependencyInjection.HttpClient;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sample.AspNetCore.Protos;
var builder = WebApplication.CreateBuilder(args);
var domain = builder.Configuration["Auth0:Domain"];
// Protect your API with authentication as you normally would
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = domain!.ToHttpsUrl();
options.Audience = builder.Configuration["Auth0:Audience"];
});
// We'll require all endpoints to be authorized by default
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
// If you're just using the authentication client and nothing else, you can use this lightweight version instead.
// builder.Services.AddAuth0AuthenticationClientCore(domain);
// Adds the AuthenticationApiClient client and provides configuration to be consumed by the management client, token cache, and IHttpClientBuilder integrations
builder.Services.AddAuth0AuthenticationClient(config =>
{
config.Domain = domain!;
config.ClientId = builder.Configuration["Auth0:ClientId"];
config.ClientSecret = builder.Configuration["Auth0:ClientSecret"];
});
// Adds the ManagementApiClient with automatic injection of the management token based on the configuration set above.
builder.Services.AddAuth0ManagementClient();
builder.Services.AddGrpc();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGrpcService<UsersService>();
app.MapGet("/users", async ([FromServices] IManagementApiClient client) =>
{
var user = await client.Users.ListAsync(new ListUsersRequestParameters() { });
return user.CurrentPage.Select(x => new Sample.AspNetCore.User(x.UserId, x.Name, x.Email)).ToArray();
});
app.MapGet("/users/org-scoped", async ([FromServices] IManagementApiClient client, HttpContext context, ILogger<Program> logger) =>
{
var orgId = context.User.FindFirstValue("org_id");
if (!string.IsNullOrEmpty(orgId)) {
logger.LogInformation("Found org {org}", orgId);
}
var user = await client.Users.ListAsync(new ListUsersRequestParameters() { });
return user.CurrentPage.Select(x => new Sample.AspNetCore.User(x.UserId, x.Name, x.Email)).ToArray();
});
app.Run();