Skip to content

Commit ad7f80e

Browse files
Merge pull request #5 from MarimerLLC/may25
May25
2 parents 431e33d + 7799d2e commit ad7f80e

File tree

16 files changed

+139
-39
lines changed

16 files changed

+139
-39
lines changed

labs/05 - State Management/BlazorHolState/BlazorHolState/BlazorHolState/Controllers/StateController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using BlazorHolState;
22
using Microsoft.AspNetCore.Mvc;
33

4-
namespace WebApi1.Controllers
4+
namespace BlazorHolState.Controllers
55
{
66
[ApiController]
77
[Route("[controller]")]

labs/06 - Authn Fundamentals/BlazorHolAuthentication/BlazorHolAuthentication/Components/Pages/Login.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<label>Password</label>
2323
<InputText type="password" @bind-Value="userInfo.Password" />
2424
</div>
25-
<button>Login</button>
25+
<button type="submit">Login</button>
2626
</EditForm>
2727
</div>
2828

labs/07 - Authn EntraId/BlazorHolEntraId/BlazorHolEntraId/Pages/Home.razor

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,27 @@
2323
following:
2424
</p>
2525

26-
<p><pre><code class="language-js">@JsonSerializer.Serialize(graphApiResponse, new JsonSerializerOptions { WriteIndented = true })</code></pre></p>
26+
<p><pre><code class="language-js">@JsonResult</code></pre></p>
2727
}
28+
</Authorized>
29+
</AuthorizeView>
2830

29-
@code {
30-
private JsonDocument? graphApiResponse = null;
31+
@code {
32+
private JsonDocument? graphApiResponse = null;
33+
private string? JsonResult { get; set; }
3134

32-
protected override async Task OnInitializedAsync()
35+
protected override async Task OnInitializedAsync()
36+
{
37+
try
3338
{
34-
try
35-
{
36-
using var response = await Http.GetAsync("https://graph.microsoft.com/v1.0/me");
37-
response.EnsureSuccessStatusCode();
38-
graphApiResponse = await response.Content.ReadFromJsonAsync<JsonDocument>().ConfigureAwait(false);
39-
}
40-
catch (AccessTokenNotAvailableException exception)
41-
{
42-
exception.Redirect();
43-
}
39+
using var response = await Http.GetAsync("https://graph.microsoft.com/v1.0/me");
40+
response.EnsureSuccessStatusCode();
41+
graphApiResponse = await response.Content.ReadFromJsonAsync<JsonDocument>().ConfigureAwait(false);
42+
JsonResult = JsonSerializer.Serialize(graphApiResponse, new JsonSerializerOptions { WriteIndented = true });
4443
}
45-
44+
catch (AccessTokenNotAvailableException exception)
45+
{
46+
exception.Redirect();
4647
}
47-
</Authorized>
48-
</AuthorizeView>
48+
}
49+
}

labs/07 - Authn EntraId/readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ builder.Services.AddScoped(sp =>
6666
await builder.Build().RunAsync();
6767
```
6868

69+
In `_Imports.razor` file, add the following code:
70+
71+
```razor
72+
@using Microsoft.AspNetCore.Components.Authorization
73+
```
74+
6975
3. Open the `wwwroot/index.html` file
7076
4. Add the following code near the bottom where other `<script>` tags exist:
7177

labs/08 - Authz/BlazorHolAuthentication/BlazorHolAuthentication/Components/Pages/Login.razor

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
<h1>Login</h1>
1414

1515
<div>
16-
<EditForm Model="userInfo" OnSubmit="LoginUser" FormName="loginform">
17-
<div>
18-
<label>Username</label>
19-
<InputText @bind-Value="userInfo.Username" />
20-
</div>
21-
<div>
22-
<label>Password</label>
23-
<InputText type="password" @bind-Value="userInfo.Password" />
24-
</div>
25-
<button>Login</button>
26-
</EditForm>
16+
<EditForm Model="userInfo" OnSubmit="LoginUser" FormName="loginform">
17+
<div>
18+
<label>Username</label>
19+
<InputText @bind-Value="userInfo.Username" />
20+
</div>
21+
<div>
22+
<label>Password</label>
23+
<InputText type="password" @bind-Value="userInfo.Password" />
24+
</div>
25+
<button>Login</button>
26+
</EditForm>
2727
</div>
2828

2929
<div style="background-color:lightgray">
30-
<p>User identities:</p>
31-
<p>admin, admin</p>
30+
<p>User identities:</p>
31+
<p>admin, admin</p>
3232
</div>
3333

3434
<div><p class="alert-danger">@Message</p></div>
@@ -65,6 +65,9 @@
6565
var roles = UserValidation.GetRoles(userInfo.Username);
6666
foreach (var item in roles)
6767
claims.Add(new Claim(ClaimTypes.Role, item));
68+
69+
claims.Add(new Claim(ClaimTypes.Country, "FR"));
70+
6871
identity.AddClaims(claims);
6972
principal = new ClaimsPrincipal(identity);
7073

labs/08 - Authz/BlazorHolAuthentication/BlazorHolAuthentication/Components/Pages/Logout.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
@page "/logout"
22

3+
@using BlazorHolAuthentication.Services
34
@using Microsoft.AspNetCore.Authentication
45
@using Microsoft.AspNetCore.Authentication.Cookies
6+
@using Microsoft.AspNetCore.Mvc
57

68
@inject IHttpContextAccessor httpContextAccessor
9+
@inject AuthenticationStateProvider authenticationStateProvider
710
@inject NavigationManager NavigationManager
811

912
<h3>Logout</h3>
@@ -16,6 +19,7 @@
1619
if (principal.Identity is not null && principal.Identity.IsAuthenticated)
1720
{
1821
await httpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
22+
((CustomAuthenticationStateProvider)authenticationStateProvider).Logout();
1923
}
2024
NavigationManager.NavigateTo("/");
2125
}

labs/08 - Authz/BlazorHolAuthentication/BlazorHolAuthentication/Components/Pages/Weather.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
<p>This component demonstrates showing data.</p>
88

9+
<AuthorizeView Policy="IsInEU">
10+
<Authorized>
11+
<p>You are EUROPEAN.</p>
12+
</Authorized>
13+
<NotAuthorized>
14+
<p>You are not European.</p>
15+
</NotAuthorized>
16+
</AuthorizeView>
17+
918
@if (forecasts == null)
1019
{
1120
<p><em>Loading...</em></p>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace BlazorHolAuthentication.Policies;
2+
3+
using Microsoft.AspNetCore.Authorization;
4+
using System.Security.Claims;
5+
6+
public class EuRequirement : IAuthorizationRequirement
7+
{
8+
// List of EU countries (simplified for demonstration)
9+
public readonly string[] EuCountries = new[] { "DE", "FR", "IT", "ES", "NL", "BE", "PL", "SE", "AT" };
10+
}
11+
12+
13+
public class EuHandler : AuthorizationHandler<EuRequirement>
14+
{
15+
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, EuRequirement requirement)
16+
{
17+
// Check if the user has a claim indicating their region or country
18+
var countryClaim = context.User.FindFirst(
19+
claim => claim.Type == ClaimTypes.Country);
20+
21+
if (countryClaim != null)
22+
{
23+
24+
if (requirement.EuCountries.Contains(countryClaim.Value))
25+
{
26+
context.Succeed(requirement);
27+
}
28+
}
29+
30+
return Task.CompletedTask;
31+
}
32+
}

labs/08 - Authz/BlazorHolAuthentication/BlazorHolAuthentication/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using BlazorHolAuthentication.Components;
2+
using BlazorHolAuthentication.Policies;
23
using BlazorHolAuthentication.Services;
34
using Microsoft.AspNetCore.Authentication.Cookies;
5+
using Microsoft.AspNetCore.Authorization;
6+
using Microsoft.AspNetCore.Components.Authorization;
7+
using Microsoft.Extensions.Options;
48
using System.Security.Claims;
59

610
var builder = WebApplication.CreateBuilder(args);
@@ -9,6 +13,14 @@
913
builder.Services.AddRazorComponents()
1014
.AddInteractiveServerComponents();
1115

16+
builder.Services.AddSingleton<AuthenticationStateProvider, CustomAuthenticationStateProvider>();
17+
18+
builder.Services.AddAuthorizationBuilder()
19+
.AddPolicy("IsAdmin", policy => policy.RequireClaim(ClaimTypes.Role, "Admin"))
20+
.AddPolicy("IsInEU", policy =>
21+
policy.Requirements.Add(new EuRequirement()));
22+
23+
builder.Services.AddSingleton<IAuthorizationHandler, EuHandler>();
1224
builder.Services.AddHttpContextAccessor();
1325
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
1426
.AddCookie();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Components.Authorization;
2+
using Microsoft.AspNetCore.Components.Server;
3+
using System.Security.Claims;
4+
5+
namespace BlazorHolAuthentication.Services
6+
{
7+
public class CustomAuthenticationStateProvider : ServerAuthenticationStateProvider
8+
{
9+
private readonly ClaimsPrincipal anonymous = new(new ClaimsIdentity());
10+
11+
public CustomAuthenticationStateProvider()
12+
{
13+
SetAuthenticationState(Task.FromResult(
14+
new AuthenticationState(anonymous)));
15+
}
16+
17+
public void Logout()
18+
{
19+
var authenticationState = Task.FromResult(
20+
new AuthenticationState(anonymous));
21+
SetAuthenticationState(authenticationState);
22+
NotifyAuthenticationStateChanged(authenticationState);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)