Last active
December 7, 2022 12:16
-
-
Save vertonghenb/17efe89c255cd7a010f27fc536d624b8 to your computer and use it in GitHub Desktop.
Blazor with Auth0, using the Management API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Auth0": { | |
"Authority": "https://<YOUR_AUTH0_DOMAIN>", | |
"ApiIdentifier": "<YOUR_API_IDENTIFIER>" | |
"ClientId": "<YOUR_M2M_CLIENT_ID>", | |
"ClientSecret": "YOUR_M2M_CLIENT_SECRET" | |
}, | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", | |
"Microsoft.AspNetCore": "Warning" | |
} | |
}, | |
"AllowedHosts": "*" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
var builder = WebApplication.CreateBuilder(args); | |
// Add services to the container. | |
builder.Services.AddAuthentication(options => | |
{ | |
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}).AddJwtBearer(options => | |
{ | |
options.Authority = builder.Configuration["Auth0:Authority"]; | |
options.Audience = builder.Configuration["Auth0:ApiIdentifier"]; | |
}); | |
// 👇 | |
builder.Services.AddAuth0AuthenticationClient(config => | |
{ | |
config.Domain = builder.Configuration["Auth0:Authority"]; | |
config.ClientId = builder.Configuration["Auth0:ClientId"]; | |
config.ClientSecret = builder.Configuration["Auth0:ClientSecret"]; | |
}); | |
builder.Services.AddAuth0ManagementClient().AddManagementAccessToken(); | |
// 👆 | |
builder.Services.AddControllersWithViews(); | |
builder.Services.AddRazorPages(); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseWebAssemblyDebugging(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Error"); | |
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseBlazorFrameworkFiles(); | |
app.UseStaticFiles(); | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.MapRazorPages(); | |
app.MapControllers(); | |
app.MapFallbackToFile("index.html"); | |
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Auth0.ManagementApi; | |
using Auth0.ManagementApi.Models; | |
using Auth0.ManagementApi.Paging; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
using WeatherStation.Shared; | |
namespace WeatherStation.Server.Controllers | |
{ | |
[ApiController] | |
[Route("[controller]")] | |
[Authorize(Roles = "Administrator")] | |
public class UserController : ControllerBase | |
{ | |
private readonly IManagementApiClient _managementApiClient; | |
public UserController(IManagementApiClient managementApiClient) | |
{ | |
_managementApiClient = managementApiClient; | |
} | |
[HttpGet] | |
public async Task<IEnumerable<UserDto.Index>> GetUsers() | |
{ | |
var users = await _managementApiClient.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo()); | |
return users.Select(x => new UserDto.Index | |
{ | |
Email = x.Email, | |
FirstName = x.FirstName, | |
LastName = x.LastName, | |
Blocked = x.Blocked ?? false, | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WeatherStation.Shared | |
{ | |
public static class UserDto | |
{ | |
public class Index | |
{ | |
public string Email { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public bool Blocked { get; set; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/users" | |
@using WeatherStation.Shared | |
@inject HttpClient Http | |
@attribute [Authorize(Roles = "Administrator")] | |
@if (users is null) | |
{ | |
<p>Loading...</p> | |
} | |
else | |
{ | |
<table class="table"> | |
<thead> | |
<tr> | |
<th scope="col">Email</th> | |
<th scope="col">Firstname</th> | |
<th scope="col">Lastname</th> | |
<th scope="col">Blocked</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach (var user in users) | |
{ | |
<tr> | |
<th scope="row">@user.Email</th> | |
<td>@user.FirstName</td> | |
<td>@user.LastName</td> | |
<td>@user.Blocked</td> | |
</tr> | |
} | |
</tbody> | |
</table> | |
} | |
@code { | |
private IEnumerable<UserDto.Index>? users; | |
protected override async Task OnInitializedAsync() | |
{ | |
users = await Http.GetFromJsonAsync<UserDto.Index[]>("user"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment