-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a17e384
commit 15854f4
Showing
39 changed files
with
235 additions
and
283 deletions.
There are no files selected for viewing
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
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
3 changes: 2 additions & 1 deletion
3
...lazorApp.Client/Components/Redirect.razor → ...ampleApp.Client/Components/Redirect.razor
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
6 changes: 4 additions & 2 deletions
6
...eBlazorApp.Client/Components/Routes.razor → ....SampleApp.Client/Components/Routes.razor
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
13 changes: 13 additions & 0 deletions
13
...itzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp.Client/Models/SignInPayload.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BitzArt.Blazor.Auth.SampleApp; | ||
|
||
public class SignInPayload | ||
{ | ||
public string? MyData { get; set; } | ||
|
||
public SignInPayload() { } | ||
|
||
public SignInPayload(string myData) | ||
{ | ||
MyData = myData; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...t.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp.Client/Pages/AuthRequiredPage.razor
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@namespace BitzArt.Blazor.Auth.SampleApp | ||
@page "/auth-required" | ||
|
||
@* This attribute makes the page require authorization. Also check `Routes.razor` *@ | ||
@attribute [Authorize] | ||
|
||
<PageTitle>Auth Required Page | Blazor.Auth</PageTitle> | ||
|
||
Congrats, you are authorized to see this page! |
37 changes: 37 additions & 0 deletions
37
...e/BitzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp.Client/Pages/HomePage.razor
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
@namespace BitzArt.Blazor.Auth.SampleApp | ||
@page "/" | ||
@rendermode InteractiveAuto | ||
|
||
<PageTitle>Home Page | Blazor.Auth</PageTitle> | ||
|
||
<h5 style="margin-bottom:2rem;"> | ||
This page is currently using @(OperatingSystem.IsBrowser() ? "WebAssembly rendering" : "server-side rendering") | ||
</h5> | ||
|
||
<AuthorizeView> | ||
|
||
<NotAuthorized> | ||
<div> | ||
<h4> | ||
You are not authenticated. | ||
</h4> | ||
|
||
<div style="margin-top:2rem;"> | ||
<button type="submit" class="btn btn-lg btn-primary" @onclick="SignInAsync">Sign In</button> | ||
</div> | ||
</div> | ||
</NotAuthorized> | ||
|
||
<Authorized> | ||
<div> | ||
<h4> | ||
You are authenticated. | ||
</h4> | ||
|
||
<div style="margin-top:2rem;"> | ||
<button type="submit" class="btn btn-lg btn-primary" @onclick="SignOutAsync">Sign Out</button> | ||
</div> | ||
</div> | ||
</Authorized> | ||
|
||
</AuthorizeView> |
22 changes: 22 additions & 0 deletions
22
...itzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp.Client/Pages/HomePage.razor.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace BitzArt.Blazor.Auth.SampleApp; | ||
|
||
public partial class HomePage | ||
{ | ||
[Inject] NavigationManager NavigationManager { get; set; } = null!; | ||
[Inject] IUserService UserService { get; set; } = null!; | ||
|
||
private async Task SignInAsync() | ||
{ | ||
var payload = new SignInPayload("some data"); | ||
var authenticationResult = await UserService.SignInAsync(payload); | ||
NavigationManager.NavigateTo("/", true); | ||
} | ||
|
||
private async Task SignOutAsync() | ||
{ | ||
await UserService.SignOutAsync(); | ||
NavigationManager.NavigateTo("/", true); | ||
} | ||
} |
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
File renamed without changes.
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
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
...azorApp/SampleBlazorApp.Client/Program.cs → ...t.Blazor.Auth.SampleApp.Client/Program.cs
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
11 changes: 6 additions & 5 deletions
11
...App/SampleBlazorApp.Client/_Imports.razor → ...azor.Auth.SampleApp.Client/_Imports.razor
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
@using System.Net.Http | ||
@namespace BitzArt.Blazor.Auth.SampleApp.Client | ||
|
||
@using System.Net.Http | ||
@using System.Net.Http.Json | ||
@using System.Text.Json | ||
|
||
@using Microsoft.AspNetCore.Components.Forms | ||
@using Microsoft.AspNetCore.Components.Routing | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using Microsoft.AspNetCore.Components.Web.Virtualization | ||
@using Microsoft.JSInterop | ||
@using Microsoft.AspNetCore.Authorization | ||
@using Microsoft.AspNetCore.Components.Authorization | ||
|
||
@using BitzArt.Blazor.Auth | ||
@using SampleBlazorApp | ||
@using SampleBlazorApp.Client | ||
@using SampleBlazorApp.Client.Components | ||
@using SampleBlazorApp.Client.Components.Layout | ||
|
||
@using static Microsoft.AspNetCore.Components.Web.RenderMode |
2 changes: 0 additions & 2 deletions
2
...ient/wwwroot/appsettings.Development.json → ...ient/wwwroot/appsettings.Development.json
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
File renamed without changes.
13 changes: 9 additions & 4 deletions
13
...rApp/SampleBlazorApp/Components/App.razor → ...p/BitzArt.Blazor.Auth.SampleApp/App.razor
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
16 changes: 16 additions & 0 deletions
16
....Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>BitzArt.Blazor.Auth.SampleApp</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\BitzArt.Blazor.Auth.Server\BitzArt.Blazor.Auth.Server.csproj" /> | ||
<ProjectReference Include="..\BitzArt.Blazor.Auth.SampleApp.Client\BitzArt.Blazor.Auth.SampleApp.Client.csproj" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
23 changes: 23 additions & 0 deletions
23
...itzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp/Properties/launchSettings.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:62230", | ||
"sslPort": 44328 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", | ||
"applicationUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
sample/BitzArt.Blazor.Auth.SampleApp/BitzArt.Blazor.Auth.SampleApp/Services/JwtService.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.IdentityModel.Tokens; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Security.Cryptography; | ||
|
||
namespace BitzArt.Blazor.Auth.SampleApp.Services; | ||
|
||
public class JwtService | ||
{ | ||
private readonly JwtSecurityTokenHandler _tokenHandler; | ||
private readonly SigningCredentials _signingCredentials; | ||
private static readonly TimeSpan _accessTokenDuration = new(0, 1, 0); | ||
private static readonly TimeSpan _refreshTokenDuration = new(1, 0, 0); | ||
|
||
public JwtService() | ||
{ | ||
var options = new JwtOptions | ||
{ | ||
PublicKey = "MIIBCgKCAQEA12zIJKpaIuNNk2yAdQ4e/EsT7al1hozyi/qFeTduf7BJFS4niFK7k9OL4VJFoUbpDt18y7Yqlz0nsEyinu/7wZJjf646yYymA8jBib/4kxQw6zH7C3qaam283k72pxb+aZOeJ6iU9KNkwTbfMHxKuTHoxySS6VH0vt3Sn0FYWryp8BVdPFlbuJp6K5otksTbdFOPgzgvwNreoI3TgA0e2clRKaEv+FGwhmY6WqR/hp/ebo0mflL2hPwJI1PLzjXdlx1sPHmYYfDTA02eJWkGYVti4oUZ9UTI5pZeRMNItSu1IyjHi45iLDQ+kRaPsx2bL/YZ7NXJu/g+dk7Lb4KdfQIDAQAB", | ||
PrivateKey = "MIIEogIBAAKCAQEA12zIJKpaIuNNk2yAdQ4e/EsT7al1hozyi/qFeTduf7BJFS4niFK7k9OL4VJFoUbpDt18y7Yqlz0nsEyinu/7wZJjf646yYymA8jBib/4kxQw6zH7C3qaam283k72pxb+aZOeJ6iU9KNkwTbfMHxKuTHoxySS6VH0vt3Sn0FYWryp8BVdPFlbuJp6K5otksTbdFOPgzgvwNreoI3TgA0e2clRKaEv+FGwhmY6WqR/hp/ebo0mflL2hPwJI1PLzjXdlx1sPHmYYfDTA02eJWkGYVti4oUZ9UTI5pZeRMNItSu1IyjHi45iLDQ+kRaPsx2bL/YZ7NXJu/g+dk7Lb4KdfQIDAQABAoIBAE6nMQvyBqbmRtSksOIMHdQPtV74mChgHc5t0X3Id1e3jXdmOpjTXBlFC7VgzHtt4HnE9GOMR1Cgy3TbBiTxigHK6PkdK+maqKKJEeCxbpiErr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5B2VqPjPZa7ewFUKluSkXy1oN8Wg1w=", | ||
}; | ||
|
||
_tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
var privateRsa = RSA.Create(); | ||
var privateKey = Convert.FromBase64String(options.PrivateKey!); | ||
privateRsa.ImportRSAPrivateKey(privateKey, out _); | ||
|
||
var privateSecurityKey = new RsaSecurityKey(privateRsa); | ||
|
||
_signingCredentials = new SigningCredentials(privateSecurityKey, SecurityAlgorithms.RsaSha256); | ||
} | ||
|
||
public JwtPair BuildJwtPair() | ||
{ | ||
var now = DateTime.UtcNow; | ||
|
||
var accessTokenExpiresAt = now + _accessTokenDuration; | ||
var accessToken = _tokenHandler.WriteToken(new JwtSecurityToken( | ||
claims: | ||
[ | ||
new Claim("myClaim", "My claim data") | ||
], | ||
notBefore: now, | ||
expires: accessTokenExpiresAt, | ||
signingCredentials: _signingCredentials | ||
)); | ||
|
||
var refreshTokenExpiresAt = now + _refreshTokenDuration; | ||
var refreshToken = _tokenHandler.WriteToken(new JwtSecurityToken( | ||
notBefore: now, | ||
expires: refreshTokenExpiresAt, | ||
signingCredentials: _signingCredentials | ||
)); | ||
|
||
return new JwtPair | ||
{ | ||
AccessToken = accessToken, | ||
RefreshToken = refreshToken, | ||
AccessTokenExpiresAt = accessTokenExpiresAt, | ||
RefreshTokenExpiresAt = refreshTokenExpiresAt | ||
}; | ||
} | ||
} | ||
|
||
internal class JwtException(string errorMessage, Exception? innerException = null) : Exception(errorMessage, innerException); | ||
|
||
internal class JwtOptions | ||
{ | ||
public required string PublicKey { get; set; } | ||
public required string PrivateKey { get; set; } | ||
public TimeSpan AccessTokenDuration { get; set; } | ||
public TimeSpan RefreshTokenDuration { get; set; } | ||
} |
Oops, something went wrong.