Skip to content

Commit ae563b5

Browse files
committed
fixes
1 parent 70de881 commit ae563b5

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

CodeFirstApi.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Client", "samples
1919
EndProject
2020
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8EC462FD-D22E-90A8-E5CE-7E832BA40C5D}"
2121
ProjectSection(SolutionItems) = preProject
22+
.github\workflows\main_codefirstapi.yml = .github\workflows\main_codefirstapi.yml
2223
readme.md = readme.md
2324
EndProjectSection
2425
EndProject
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
@page "/auth"
2+
@inject IClass1 Service
23

34
@using Microsoft.AspNetCore.Authorization
45

5-
@attribute [Authorize]
6-
76
<PageTitle>Auth</PageTitle>
7+
<AuthorizeView>
8+
<Authorized>
9+
<h1>You are authenticated</h1>
10+
Hello @context.User.Identity?.Name!
11+
The counter below is will not throw an exception if you click it, as authorization is checked on server side if all render and you are authorized.
12+
</Authorized>
13+
<NotAuthorized>
14+
<h1>You're not authorized</h1>
15+
The counter below is expected to throw an exception if you click it, as authorization is checked on server side if all render modes.
16+
</NotAuthorized>
17+
</AuthorizeView>
818

9-
<h1>You are authenticated</h1>
1019

11-
<AuthorizeView>
12-
Hello @context.User.Identity?.Name!
13-
</AuthorizeView>
20+
<h2>Authorized Counter</h2>
21+
<p role="status">Current count: @currentCount</p>
22+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
23+
24+
@code {
25+
private int currentCount = 0;
26+
27+
private async Task IncrementCount()
28+
{
29+
currentCount = await Service.IncrementAuthorizedCountAsync(currentCount);
30+
}
31+
}

samples/BlazorApp.Shared/IClass1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface IClass1
99
[PersistForPrerendering]
1010
ValueTask<IReadOnlyList<WeatherForecast>> GetForecastsAsync();
1111
ValueTask<int> IncrementCountAsync(int currentCount);
12+
[Authorize]
13+
ValueTask<int> IncrementAuthorizedCountAsync(int currentCount);
1214
}

samples/BlazorApp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
app.UseAntiforgery();
6969

7070
app.MapStaticAssets();
71+
app.MapControllers();
7172
app.MapRazorComponents<App>()
7273
.AddInteractiveServerRenderMode()
7374
.AddInteractiveWebAssemblyRenderMode()

samples/BlazorApp/Services/Class1Implementation.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ public class Class1Implementation : IClass1
88
{
99
public async ValueTask<IReadOnlyList<WeatherForecast>> GetForecastsAsync()
1010
{
11-
// Simulate asynchronous loading to demonstrate a loading indicator
12-
await Task.Delay(500);
13-
1411
var startDate = DateOnly.FromDateTime(DateTime.Now);
1512
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
1613
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
@@ -25,4 +22,9 @@ public async ValueTask<int> IncrementCountAsync(int currentCount)
2522
{
2623
return currentCount + 1;
2724
}
25+
26+
public async ValueTask<int> IncrementAuthorizedCountAsync(int currentCount)
27+
{
28+
return currentCount + 1;
29+
}
2830
}

0 commit comments

Comments
 (0)