63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Hcs.WebApp.Identity
|
|
{
|
|
internal sealed class IdentityRedirectManager(NavigationManager navigationManager)
|
|
{
|
|
public const string STATUS_COOKIE_NAME = "Identity.StatusMessage";
|
|
|
|
private static readonly CookieBuilder statusCookieBuilder = new()
|
|
{
|
|
SameSite = SameSiteMode.Strict,
|
|
HttpOnly = true,
|
|
IsEssential = true,
|
|
MaxAge = TimeSpan.FromSeconds(5),
|
|
};
|
|
|
|
private string CurrentPath => navigationManager.ToAbsoluteUri(navigationManager.Uri).GetLeftPart(UriPartial.Path);
|
|
|
|
[DoesNotReturn]
|
|
public void RedirectTo(string? uri)
|
|
{
|
|
uri ??= "";
|
|
|
|
if (!Uri.IsWellFormedUriString(uri, UriKind.Relative))
|
|
{
|
|
uri = navigationManager.ToBaseRelativePath(uri);
|
|
}
|
|
|
|
navigationManager.NavigateTo(uri);
|
|
|
|
throw new InvalidOperationException($"{nameof(IdentityRedirectManager)} может быть использован только при статичном рендеринге");
|
|
}
|
|
|
|
[DoesNotReturn]
|
|
public void RedirectTo(string uri, Dictionary<string, object?> queryParameters)
|
|
{
|
|
var uriWithoutQuery = navigationManager.ToAbsoluteUri(uri).GetLeftPart(UriPartial.Path);
|
|
var newUri = navigationManager.GetUriWithQueryParameters(uriWithoutQuery, queryParameters);
|
|
RedirectTo(newUri);
|
|
}
|
|
|
|
[DoesNotReturn]
|
|
public void RedirectToWithStatus(string uri, string message, HttpContext context)
|
|
{
|
|
context.Response.Cookies.Append(STATUS_COOKIE_NAME, message, statusCookieBuilder.Build(context));
|
|
RedirectTo(uri);
|
|
}
|
|
|
|
[DoesNotReturn]
|
|
public void RedirectToCurrentPage()
|
|
{
|
|
RedirectTo(CurrentPath);
|
|
}
|
|
|
|
[DoesNotReturn]
|
|
public void RedirectToCurrentPageWithStatus(string message, HttpContext context)
|
|
{
|
|
RedirectToWithStatus(CurrentPath, message, context);
|
|
}
|
|
}
|
|
}
|