Skip to content

Commit 0f23b59

Browse files
committed
#165 Added support for client authentication with Auth0
1 parent 0421ecc commit 0f23b59

File tree

11 files changed

+96
-24
lines changed

11 files changed

+96
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,4 @@ MigrationBackup/
366366
**/lcov.info
367367
/src/.cr/personal
368368
/src/global.json
369+
/src/AdminAssistant.Blazor/Client/wwwroot/appsettings.json

src/AdminAssistant.Blazor/Client/AdminAssistant.Blazor.Client.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="9.5.3" />
3333

3434
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.4" />
35+
36+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="5.0.4" />
3537
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.4" PrivateAssets="all" />
3638
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
3739
<PackageReference Include="Syncfusion.Blazor" Version="19.1.0.54" />
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<Router AppAssembly="@typeof(Program).Assembly">
2-
<Found Context="routeData">
3-
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4-
</Found>
5-
<NotFound>
6-
<LayoutView Layout="@typeof(MainLayout)">
7-
<p>Sorry, there's nothing at this address.</p>
8-
</LayoutView>
9-
</NotFound>
2+
<Found Context="routeData">
3+
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
4+
<Authorizing>
5+
<p>Determining session state, please wait...</p>
6+
</Authorizing>
7+
<NotAuthorized>
8+
<h1>Sorry</h1>
9+
<p>You're not authorized to reach this page. You need to log in.</p>
10+
</NotAuthorized>
11+
</AuthorizeRouteView>
12+
</Found>
13+
<NotFound>
14+
<LayoutView Layout="@typeof(MainLayout)">
15+
<p>Sorry, there's nothing at this address.</p>
16+
</LayoutView>
17+
</NotFound>
1018
</Router>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@page "/authentication/{action}"
2+
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
3+
@using Microsoft.Extensions.Configuration
4+
5+
@inject NavigationManager Navigation
6+
@inject IConfiguration Configuration
7+
8+
<RemoteAuthenticatorView Action="@Action">
9+
<LogOut>
10+
@{
11+
var authority = (string)Configuration["Auth0:Authority"];
12+
var clientId = (string)Configuration["Auth0:ClientId"];
13+
14+
Navigation.NavigateTo($"{authority}/v2/logout?client_id={clientId}");
15+
}
16+
</LogOut>
17+
</RemoteAuthenticatorView>
18+
19+
@code{
20+
[Parameter] public string Action { get; set; }
21+
}

src/AdminAssistant.Blazor/Client/Modules/AccountsModule/Accounts.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@page "/accounts"
2+
@attribute [Authorize]
23
@inject AdminAssistant.UI.Modules.AccountsModule.IAccountsViewModel vm
34

45
<div class="cl-accounts">

src/AdminAssistant.Blazor/Client/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Threading.Tasks;
44
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
5+
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Logging;
78
using Blazor.Extensions.Logging;
@@ -40,6 +41,11 @@ public static async Task Main(string[] args)
4041
// TODO: Configure other production logging options.
4142
#endif
4243
});
44+
builder.Services.AddOidcAuthentication(options =>
45+
{
46+
builder.Configuration.Bind("Auth0", options.ProviderOptions);
47+
options.ProviderOptions.ResponseType = "code";
48+
});
4349

4450
// See https://github.com/ryanelian/FluentValidation.Blazor
4551
builder.Services.AddValidatorsFromAssemblyContaining<Infra.DAL.IDatabasePersistable>();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@using Microsoft.AspNetCore.Components.Authorization
2+
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
3+
4+
@inject NavigationManager Navigation
5+
@inject SignOutSessionStateManager SignOutManager
6+
7+
<AuthorizeView>
8+
<Authorized>
9+
Hello, @context.User.Identity.Name!
10+
<a href="#" @onclick="BeginSignOut">Log out</a>
11+
</Authorized>
12+
<NotAuthorized>
13+
<a href="authentication/login">Log in</a>
14+
</NotAuthorized>
15+
</AuthorizeView>
16+
17+
@code{
18+
private async Task BeginSignOut(MouseEventArgs args)
19+
{
20+
await SignOutManager.SetSignOutState();
21+
Navigation.NavigateTo("authentication/logout");
22+
}
23+
}

src/AdminAssistant.Blazor/Client/Shared/MainLayout.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<div class="cl-app">
66

77
<!-- Start Header -->
8+
<AccessControl />
89
<!--<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
910
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbar2" aria-controls="navbar2" aria-expanded="false" aria-label="Toggle navigation">
1011
<span class="navbar-toggler-icon"></span>

src/AdminAssistant.Blazor/Client/_Imports.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@using System.Net.Http
2+
@using Microsoft.AspNetCore.Authorization
3+
@using Microsoft.AspNetCore.Components.Authorization
24
@using Microsoft.AspNetCore.Components.Forms
35
@using Microsoft.AspNetCore.Components.Routing
46
@using Microsoft.AspNetCore.Components.Web
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Auth0": {
3+
"Authority": "https://<YOUR_AUTH0_DOMAIN>",
4+
"ClientId": "<YOUR_CLIENT_ID>"
5+
}
6+
}

src/AdminAssistant.Blazor/Client/wwwroot/index.html

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
<link href="/assets/css/site.css" rel="stylesheet" />
1010
<script src="_content/Blazor.Extensions.Logging/blazor.extensions.logging.js" defer></script>
1111
</head>
12-
<body>
13-
<app>
14-
<div class="loading">
15-
<p>Loading ...</p>
16-
<div class="loading-bar"></div>
17-
<div class="loading-bar"></div>
18-
<div class="loading-bar"></div>
19-
<div class="loading-bar"></div>
20-
</div>
21-
</app>
22-
<div id="blazor-error-ui">
23-
An unhandled error has occurred.
24-
<a href="" class="reload">Reload</a>
25-
<a class="dismiss">🗙</a>
12+
<body>
13+
<app>
14+
<div class="loading">
15+
<p>Loading ...</p>
16+
<div class="loading-bar"></div>
17+
<div class="loading-bar"></div>
18+
<div class="loading-bar"></div>
19+
<div class="loading-bar"></div>
2620
</div>
27-
<script src="_framework/blazor.webassembly.js"></script>
28-
</body>
21+
</app>
22+
<div id="blazor-error-ui">
23+
An unhandled error has occurred.
24+
<a href="" class="reload">Reload</a>
25+
<a class="dismiss">🗙</a>
26+
</div>
27+
<script src="\_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
28+
<script src="_framework/blazor.webassembly.js"></script>
29+
</body>
2930
</html>

0 commit comments

Comments
 (0)