Blazor
Setup
App.razor
@inject NavigationManager UriHelper
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
var returnUrl = UriHelper.ToBaseRelativePath(UriHelper.Uri);
UriHelper.NavigateTo($"login?redirectUri={returnUrl}", forceLoad: true);
}
</NotAuthorized>
<Authorizing>
Loading...
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
MainLayout.razor
@inherits LayoutComponentBase
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<AuthorizeView>
<Authorized>
<form method="get" action="logout">
<button type="submit" class="nav-link btn btn-link">Log out</button>
</form>
</Authorized>
<NotAuthorized>
<a href="login?redirectUri=/" class="nav-link btn btn-link">Log in</a>
</NotAuthorized>
</AuthorizeView>
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<article class="content px-4">
@Body
</article>
</main>
</div>
Index.razor
@page "/"
<PageTitle>Index</PageTitle>
<AuthorizeView>
<Authorized>
<h1>Hello @context.User.Identity.Name!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
</Authorized>
<NotAuthorized>
<h1>Not Authorized, please log in</h1>
</NotAuthorized>
</AuthorizeView>
Login
Logout
Logout.cshtml.cs
public class LogoutModel : PageModel
{
public async Task<IActionResult> OnGetAsync()
{
var result = await HttpContext.AuthenticateAsync();
var properties = result.Properties;
var provider = properties.Items[".AuthScheme"];
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(provider, properties);
return Redirect("/");
}
}