Last active
October 3, 2024 14:09
-
-
Save vertonghenb/77c5c6baa4c5d7ab31a2e35c65f8dfbf to your computer and use it in GitHub Desktop.
Authenticated Weather Station
This file contains hidden or 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 System.Net.Http | |
| @using System.Net.Http.Json | |
| @using Microsoft.AspNetCore.Components.Forms | |
| @using Microsoft.AspNetCore.Components.Routing | |
| @using Microsoft.AspNetCore.Components.Web | |
| @using Microsoft.AspNetCore.Components.Web.Virtualization | |
| @using Microsoft.AspNetCore.Components.WebAssembly.Http | |
| @using Microsoft.JSInterop | |
| @using Client | |
| @using Client.Layout | |
| // π | |
| @using Client.Auth | |
| @using Microsoft.AspNetCore.Components.Authorization | |
| @using Microsoft.AspNetCore.Authorization | |
| // π |
This file contains hidden or 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.Components.WebAssembly.Authentication | |
| @inject NavigationManager Navigation | |
| @inject SignOutSessionStateManager SignOutManager | |
| <AuthorizeView> | |
| <Authorized> | |
| Hello, @context.User.Identity.Name! | |
| <a href="#" @onclick="BeginSignOut">Log out</a> | |
| </Authorized> | |
| <NotAuthorized> | |
| <a href="authentication/login">Log in</a> | |
| </NotAuthorized> | |
| </AuthorizeView> | |
| @code{ | |
| private async Task BeginSignOut(MouseEventArgs args) | |
| { | |
| await SignOutManager.SetSignOutState(); | |
| Navigation.NavigateTo("authentication/logout"); | |
| } | |
| } |
This file contains hidden or 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
| <Router AppAssembly="@typeof(App).Assembly"> | |
| <Found Context="routeData"> | |
| // π | |
| <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> | |
| <NotAuthorized> | |
| @if (context.User.Identity?.IsAuthenticated != true) | |
| { | |
| <RedirectToLogin /> | |
| } | |
| else | |
| { | |
| <p role="alert">You are not authorized to access this resource.</p> | |
| } | |
| </NotAuthorized> | |
| </AuthorizeRouteView> | |
| // π | |
| <FocusOnNavigate RouteData="@routeData" Selector="h1" /> | |
| </Found> | |
| <NotFound> | |
| <PageTitle>Not found</PageTitle> | |
| <LayoutView Layout="@typeof(MainLayout)"> | |
| <p role="alert">Sorry, there's nothing at this address.</p> | |
| </LayoutView> | |
| </NotFound> | |
| </Router> |
This file contains hidden or 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>", | |
| "ClientId": "<YOUR_CLIENT_ID>" | |
| } | |
| } |
This file contains hidden or 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 "/authentication/{action}" | |
| @using Microsoft.AspNetCore.Components.WebAssembly.Authentication | |
| <RemoteAuthenticatorView Action="@Action" /> | |
| @code { | |
| [Parameter] public string? Action { get; set; } | |
| } |
This file contains hidden or 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://**********.eu.auth0.com", | |
| "ClientId": "Pbwsbr9N1s*************************", | |
| "Audience": "<YOUR_API_IDENTIFIER>" // π Remove this comment btw, since JSON cannot parse this π’ | |
| } | |
| } |
This file contains hidden or 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.Components.Web; | |
| using Microsoft.AspNetCore.Components.WebAssembly.Authentication; // π | |
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | |
| using WeatherStation.Client; | |
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | |
| builder.RootComponents.Add<App>("#app"); | |
| builder.RootComponents.Add<HeadOutlet>("head::after"); | |
| // π | |
| builder.Services.AddHttpClient("WeatherAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) | |
| .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>(); | |
| builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>() | |
| .CreateClient("WeatherAPI")); | |
| // π | |
| builder.Services.AddOidcAuthentication(options => | |
| { | |
| builder.Configuration.Bind("Auth0", options.ProviderOptions); | |
| options.ProviderOptions.ResponseType = "code"; | |
| options.ProviderOptions.AdditionalProviderParameters.Add("audience", builder.Configuration["Auth0:Audience"]); | |
| }); | |
| await builder.Build().RunAsync(); |
This file contains hidden or 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.Components.Web; | |
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | |
| using Client; | |
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | |
| builder.RootComponents.Add<App>("#app"); | |
| builder.RootComponents.Add<HeadOutlet>("head::after"); | |
| builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | |
| builder.Services.AddCascadingAuthenticationState(); | |
| builder.Services.AddOidcAuthentication(options => | |
| { | |
| builder.Configuration.Bind("Auth0", options.ProviderOptions); | |
| options.ProviderOptions.ResponseType = "code"; | |
| options.ProviderOptions.PostLogoutRedirectUri = builder.HostEnvironment.BaseAddress; | |
| options.ProviderOptions.AdditionalProviderParameters.Add("audience", builder.Configuration["Auth0:Audience"]!); // π | |
| }); | |
| await builder.Build().RunAsync(); |
This file contains hidden or 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 "/weather" | |
| @attribute [Authorize] // π | |
| @using Shared | |
| @inject HttpClient Http | |
| <PageTitle>Weather</PageTitle> | |
| <h1>Weather</h1> | |
| <p>This component demonstrates fetching data from the server.</p> | |
| @if (forecasts == null) | |
| { | |
| <p><em>Loading...</em></p> | |
| } | |
| else | |
| { | |
| <table class="table"> | |
| <thead> | |
| <tr> | |
| <th>Date</th> | |
| <th>Temp. (C)</th> | |
| <th>Temp. (F)</th> | |
| <th>Summary</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| @foreach (var forecast in forecasts) | |
| { | |
| <tr> | |
| <td>@forecast.Date.ToShortDateString()</td> | |
| <td>@forecast.TemperatureC</td> | |
| <td>@forecast.TemperatureF</td> | |
| <td>@forecast.Summary</td> | |
| </tr> | |
| } | |
| </tbody> | |
| </table> | |
| } | |
| @code { | |
| private WeatherForecast[]? forecasts; | |
| protected override async Task OnInitializedAsync() | |
| { | |
| forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("api/WeatherForecast"); | |
| } | |
| } |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Client</title> | |
| <base href="/" /> | |
| <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> | |
| <link rel="stylesheet" href="css/app.css" /> | |
| <link rel="icon" type="image/png" href="favicon.png" /> | |
| <link href="Client.styles.css" rel="stylesheet" /> | |
| </head> | |
| <body> | |
| <div id="app"> | |
| <svg class="loading-progress"> | |
| <circle r="40%" cx="50%" cy="50%" /> | |
| <circle r="40%" cx="50%" cy="50%" /> | |
| </svg> | |
| <div class="loading-progress-text"></div> | |
| </div> | |
| <div id="blazor-error-ui"> | |
| An unhandled error has occurred. | |
| <a href="" class="reload">Reload</a> | |
| <a class="dismiss">π</a> | |
| </div> | |
| <!-- π --> | |
| <script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script> | |
| <!-- π --> | |
| <script src="_framework/blazor.webassembly.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| @inherits LayoutComponentBase | |
| <div class="page"> | |
| <div class="sidebar"> | |
| <NavMenu /> | |
| </div> | |
| <main> | |
| <div class="top-row px-4"> | |
| <LoginDisplay /> // π | |
| <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a> | |
| </div> | |
| <article class="content px-4"> | |
| @Body | |
| </article> | |
| </main> | |
| </div> |
This file contains hidden or 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.Components.Web; | |
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | |
| using Client; | |
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | |
| builder.RootComponents.Add<App>("#app"); | |
| builder.RootComponents.Add<HeadOutlet>("head::after"); | |
| builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); | |
| // π | |
| builder.Services.AddCascadingAuthenticationState(); | |
| builder.Services.AddOidcAuthentication(options => | |
| { | |
| builder.Configuration.Bind("Auth0", options.ProviderOptions); | |
| options.ProviderOptions.ResponseType = "code"; | |
| options.ProviderOptions.PostLogoutRedirectUri = builder.HostEnvironment.BaseAddress; | |
| }); | |
| // π | |
| await builder.Build().RunAsync(); |
This file contains hidden or 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.Components.WebAssembly.Authentication | |
| @inject NavigationManager Navigation | |
| @code { | |
| protected override void OnInitialized() | |
| { | |
| Navigation.NavigateToLogin("authentication/login"); | |
| } | |
| } |
This file contains hidden or 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>", | |
| "Audience": "<YOUR_API_IDENTIFIER>" | |
| }, | |
| // π | |
| "Logging": { | |
| "LogLevel": { | |
| "Default": "Information", | |
| "Microsoft.AspNetCore": "Warning" | |
| } | |
| }, | |
| "AllowedHosts": "*" | |
| } |
This file contains hidden or 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 System.Security.Claims; | |
| using Microsoft.AspNetCore.Authentication.JwtBearer; | |
| using Microsoft.IdentityModel.Tokens; | |
| // π | |
| var builder = WebApplication.CreateBuilder(args); | |
| // Add services to the container. | |
| builder.Services.AddControllers(); | |
| // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | |
| builder.Services.AddEndpointsApiExplorer(); | |
| builder.Services.AddSwaggerGen(); | |
| // π | |
| 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:Audience"]; | |
| options.TokenValidationParameters = new TokenValidationParameters | |
| { | |
| NameClaimType = ClaimTypes.NameIdentifier | |
| }; | |
| }); | |
| // π | |
| var app = builder.Build(); | |
| // Configure the HTTP request pipeline. | |
| if (app.Environment.IsDevelopment()) | |
| { | |
| app.UseSwagger(); | |
| app.UseSwaggerUI(); | |
| } | |
| app.UseHttpsRedirection(); | |
| app.UseBlazorFrameworkFiles(); | |
| app.UseStaticFiles(); | |
| // π | |
| app.UseRouting(); | |
| app.UseAuthentication(); | |
| app.UseAuthorization(); // Authorization ALWAYS after Authentication, both after UseRouting(); | |
| // π | |
| app.MapControllers(); | |
| app.MapFallbackToFile("index.html"); | |
| app.Run(); |
This file contains hidden or 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.Mvc; | |
| using WeatherStation.Shared; | |
| namespace WeatherStation.Server.Controllers; | |
| [ApiController] | |
| [Route("[controller]")] | |
| public class WeatherForecastController : ControllerBase | |
| { | |
| private static readonly string[] Summaries = new[] | |
| { | |
| "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
| }; | |
| private static readonly List<WeatherForecast> forecasts = new(); | |
| static WeatherForecastController() | |
| { | |
| forecasts.AddRange(Enumerable.Range(1, 5).Select(index => new WeatherForecast | |
| { | |
| Date = DateTime.Now.AddDays(index), | |
| TemperatureC = Random.Shared.Next(-20, 55), | |
| Summary = Summaries[Random.Shared.Next(Summaries.Length)] | |
| })); | |
| } | |
| [HttpGet] | |
| public IEnumerable<WeatherForecast> GetForecasts() | |
| { | |
| return forecasts; | |
| } | |
| [HttpPost] | |
| public WeatherForecast CreateForecast(WeatherForecast forecast) | |
| { | |
| forecasts.Add(forecast); | |
| return forecast; | |
| } | |
| } |
This file contains hidden or 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.Authorization; // π | |
| using Microsoft.AspNetCore.Mvc; | |
| using Shared; | |
| namespace Server.Controllers; | |
| [ApiController] | |
| [Route("api/[controller]")] | |
| [Authorize] // π | |
| public class WeatherForecastController : ControllerBase | |
| { | |
| private static readonly string[] Summaries = new[] | |
| { | |
| "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
| }; | |
| private static readonly List<WeatherForecast> forecasts = new(); | |
| static WeatherForecastController() | |
| { | |
| forecasts.AddRange(Enumerable.Range(1, 5).Select(index => new WeatherForecast | |
| { | |
| Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | |
| TemperatureC = Random.Shared.Next(-20, 55), | |
| Summary = Summaries[Random.Shared.Next(Summaries.Length)] | |
| })); | |
| } | |
| [HttpGet] | |
| public IEnumerable<WeatherForecast> GetForecasts() | |
| { | |
| return forecasts; | |
| } | |
| [HttpPost] | |
| public WeatherForecast CreateForecast(WeatherForecast forecast) | |
| { | |
| forecasts.Add(forecast); | |
| return forecast; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment