-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
30 changed files
with
454 additions
and
338 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,12 @@ | ||
<CascadingAuthenticationState> | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<CascadingAuthenticationState> | ||
<Router AppAssembly="@typeof(Program).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" /> | ||
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
<p>Sorry, there's nothing at this address.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> | ||
</CascadingAuthenticationState> | ||
</CascadingAuthenticationState> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
@page "/fetchprotecteddata" | ||
@using BlazorShared; | ||
@using Microsoft.AspNetCore.Authorization | ||
@attribute [Authorize] | ||
@inject Services.FetchWeatherForecastService WeatherService | ||
|
||
<h1>Weather forecast</h1> | ||
|
||
<p>This component demonstrates fetching data from the server.</p> | ||
|
||
@if (forecasts == null) | ||
{ | ||
<p><em>Loading...</em></p> | ||
} | ||
else | ||
{ | ||
<div> | ||
<button @onclick="OnRefreshClick">Refresh</button> | ||
</div> | ||
|
||
<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() | ||
{ | ||
await LoadData(); | ||
} | ||
|
||
private async Task OnRefreshClick() | ||
{ | ||
await LoadData(); | ||
} | ||
|
||
private async Task LoadData() | ||
{ | ||
forecasts = null; | ||
forecasts = await WeatherService.GetProtectedWeatherForeacast(); | ||
} | ||
} |
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,5 +1,17 @@ | ||
@page "/" | ||
|
||
<PageTitle>Index</PageTitle> | ||
<h1>Hello, world!</h1> | ||
|
||
<CurrentSession /> | ||
Welcome to your new app. | ||
|
||
<SurveyPrompt Title="How is Blazor working for you?" /> | ||
|
||
<AuthorizeView> | ||
<Authorized> | ||
<strong>Hello, @context!.User!.Identity!.Name!</strong> | ||
<a href="Account/Logout">Log out</a> | ||
</Authorized> | ||
<NotAuthorized> | ||
<a href="Account/Login">Log in</a> | ||
</NotAuthorized> | ||
</AuthorizeView> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Net; | ||
|
||
namespace BlazorApp.Services; | ||
|
||
public class AuthorizedHandler : DelegatingHandler | ||
{ | ||
private readonly HostAuthenticationStateProvider _authenticationStateProvider; | ||
|
||
public AuthorizedHandler(HostAuthenticationStateProvider authenticationStateProvider) | ||
{ | ||
_authenticationStateProvider = authenticationStateProvider; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); | ||
HttpResponseMessage responseMessage; | ||
if (!authState!.User!.Identity!.IsAuthenticated) | ||
{ | ||
// if user is not authenticated, immediately set response status to 401 Unauthorized | ||
responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized); | ||
} | ||
else | ||
{ | ||
responseMessage = await base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
if (responseMessage.StatusCode == HttpStatusCode.Unauthorized) | ||
{ | ||
// if server returned 401 Unauthorized, redirect to login page | ||
_authenticationStateProvider.SignIn(); | ||
} | ||
|
||
return responseMessage; | ||
} | ||
} |
Oops, something went wrong.