oqtane.framework/Oqtane.Client/Themes/Controls/Login.razor
2019-09-15 12:21:32 +02:00

59 lines
1.9 KiB
Plaintext

@using Oqtane.Themes
@using Oqtane.Services
@using Oqtane.Providers
@using Oqtane.Shared
@using Microsoft.JSInterop
@namespace Oqtane.Themes.Controls
@inherits ThemeObjectBase
@inject NavigationManager NavigationManager
@inject IUserService UserService
@inject IJSRuntime jsRuntime
@inject IServiceProvider ServiceProvider
<AuthorizeView>
<Authorizing>
<text>...</text>
</Authorizing>
<Authorized>
<button type="button" class="btn btn-primary" @onclick="LogoutUser">Logout</button>
</Authorized>
<NotAuthorized>
<button type="button" class="btn btn-primary" @onclick="LoginUser">Login</button>
</NotAuthorized>
</AuthorizeView>
@code {
private void LoginUser()
{
string returnurl = PageState.Alias.Path;
if (PageState.Page.Path != "/")
{
returnurl += "/" + PageState.Page.Path;
}
NavigationManager.NavigateTo("login?returnurl=" + returnurl);
}
private async Task LogoutUser()
{
await UserService.LogoutUserAsync();
var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider));
if (authstateprovider == null)
{
// server-side Blazor
var interop = new Interop(jsRuntime);
string antiforgerytoken = await interop.GetElementByName("__RequestVerificationToken");
var fields = new { __RequestVerificationToken = antiforgerytoken, returnurl = (PageState.Alias.Path + "/" + PageState.Page.Path) };
await interop.SubmitForm("/logout/", fields);
}
else
{
// client-side Blazor
authstateprovider.NotifyAuthenticationChanged();
PageState.Reload = Constants.ReloadSite;
NavigationManager.NavigateTo(NavigateUrl(PageState.Page.Path, "logout"));
}
}
}