Update registration
This commit is contained in:
@ -3,8 +3,7 @@
|
|||||||
@implements IDisposable
|
@implements IDisposable
|
||||||
|
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
@inject NotificationService NotificationService
|
||||||
<RadzenComponents @rendermode="InteractiveServer" />
|
|
||||||
|
|
||||||
<RadzenLayout Style="grid-template-areas: 'rz-sidebar rz-header' 'rz-sidebar rz-body'">
|
<RadzenLayout Style="grid-template-areas: 'rz-sidebar rz-header' 'rz-sidebar rz-body'">
|
||||||
<RadzenHeader>
|
<RadzenHeader>
|
||||||
@ -25,11 +24,7 @@
|
|||||||
<AuthorizeView>
|
<AuthorizeView>
|
||||||
<Authorized>
|
<Authorized>
|
||||||
<RadzenPanelMenuItem Text="@context.User.Identity?.Name" />
|
<RadzenPanelMenuItem Text="@context.User.Identity?.Name" />
|
||||||
<RadzenPanelMenuItem Path="javascript:document.forms['logout'].submit();" Text="Выйти" Icon="logout" />
|
<RadzenPanelMenuItem Path="/identity/logout" Text="Выйти" Icon="logout" />
|
||||||
<form action="account/logout" method="post" name="logout">
|
|
||||||
<AntiforgeryToken />
|
|
||||||
<input type="hidden" name="ReturnUrl" value="@currentUrl" />
|
|
||||||
</form>
|
|
||||||
</Authorized>
|
</Authorized>
|
||||||
<NotAuthorized>
|
<NotAuthorized>
|
||||||
<RadzenPanelMenuItem Path="/account/register" Text="Регистрация" Icon="person_add" />
|
<RadzenPanelMenuItem Path="/account/register" Text="Регистрация" Icon="person_add" />
|
||||||
@ -40,6 +35,7 @@
|
|||||||
</RadzenStack>
|
</RadzenStack>
|
||||||
</RadzenSidebar>
|
</RadzenSidebar>
|
||||||
<RadzenBody>
|
<RadzenBody>
|
||||||
|
<RadzenNotification Style="position: absolute;top: 0; right: 0;" />
|
||||||
@Body
|
@Body
|
||||||
<div id="blazor-error-ui">
|
<div id="blazor-error-ui">
|
||||||
Произошла непредвиденная ошибка
|
Произошла непредвиденная ошибка
|
||||||
@ -69,6 +65,8 @@
|
|||||||
{
|
{
|
||||||
currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
|
currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
|
||||||
|
|
||||||
|
NotificationService.Messages.Clear();
|
||||||
|
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,13 @@
|
|||||||
@page "/account/register"
|
@page "/account/register"
|
||||||
|
|
||||||
@using Microsoft.AspNetCore.Identity
|
|
||||||
@using Hcs.WebApp.Data
|
|
||||||
@using Hcs.WebApp.Identity
|
|
||||||
|
|
||||||
@inject IUserStore<AppUser> UserStore
|
|
||||||
@inject UserManager<AppUser> UserManager
|
|
||||||
@inject NotificationService NotificationService
|
@inject NotificationService NotificationService
|
||||||
@inject SignInManager<AppUser> SignInManager
|
|
||||||
@inject IdentityRedirectManager RedirectManager
|
|
||||||
|
|
||||||
<PageTitle>Регистрация аккаунта</PageTitle>
|
<PageTitle>Регистрация аккаунта</PageTitle>
|
||||||
|
|
||||||
<RadzenCard class="rz-mx-auto" Style="max-width: 420px">
|
<RadzenCard class="rz-mx-auto" Style="max-width: 420px">
|
||||||
<RadzenTemplateForm TItem="InputModel" Data=@Input Method="post" Submit=@OnSubmit>
|
<RadzenTemplateForm TItem="InputModel" Data=@Input Method="post" Action="@($"identity/register?returnUrl={ReturnUrl}")">
|
||||||
<RadzenStack Gap="1rem" class="rz-p-sm-12">
|
<RadzenStack Gap="1rem" class="rz-p-sm-12">
|
||||||
<RadzenText TextStyle="TextStyle.H4" TextAlign="TextAlign.Center">Регистрация</RadzenText>
|
<RadzenText TextStyle="TextStyle.H5" TextAlign="TextAlign.Center">Регистрация</RadzenText>
|
||||||
<RadzenFormField Text="Логин" Variant="Variant.Outlined">
|
<RadzenFormField Text="Логин" Variant="Variant.Outlined">
|
||||||
<ChildContent>
|
<ChildContent>
|
||||||
<RadzenTextBox Name="UserName" @bind-Value=@Input.UserName AutoCompleteType="AutoCompleteType.Username" />
|
<RadzenTextBox Name="UserName" @bind-Value=@Input.UserName AutoCompleteType="AutoCompleteType.Username" />
|
||||||
@ -42,7 +34,7 @@
|
|||||||
</ChildContent>
|
</ChildContent>
|
||||||
<Helper>
|
<Helper>
|
||||||
<RadzenRequiredValidator Component="ConfirmPassword" Text="Поле 'Пароль' обязательно к заполнению" />
|
<RadzenRequiredValidator Component="ConfirmPassword" Text="Поле 'Пароль' обязательно к заполнению" />
|
||||||
<RadzenCompareValidator Visible=@(!string.IsNullOrEmpty(Input.ConfirmPassword)) Value=@Input.Password Component="ConfirmPassword" Text="Пароли должны совпадать" />
|
<RadzenCompareValidator Value=@Input.Password Component="ConfirmPassword" Text="Пароли должны совпадать" />
|
||||||
</Helper>
|
</Helper>
|
||||||
</RadzenFormField>
|
</RadzenFormField>
|
||||||
<RadzenButton ButtonType="ButtonType.Submit" Text="Зарегистрировать"></RadzenButton>
|
<RadzenButton ButtonType="ButtonType.Submit" Text="Зарегистрировать"></RadzenButton>
|
||||||
@ -63,28 +55,28 @@
|
|||||||
[SupplyParameterFromForm]
|
[SupplyParameterFromForm]
|
||||||
InputModel Input { get; set; } = new();
|
InputModel Input { get; set; } = new();
|
||||||
|
|
||||||
|
[SupplyParameterFromQuery]
|
||||||
|
string? Errors { get; set; }
|
||||||
|
|
||||||
[SupplyParameterFromQuery]
|
[SupplyParameterFromQuery]
|
||||||
string? ReturnUrl { get; set; }
|
string? ReturnUrl { get; set; }
|
||||||
|
|
||||||
async Task OnSubmit(InputModel mode)
|
protected override void OnAfterRender(bool firstRender)
|
||||||
{
|
{
|
||||||
var user = Activator.CreateInstance<AppUser>();
|
base.OnAfterRender(firstRender);
|
||||||
await UserStore.SetUserNameAsync(user, Input.UserName, CancellationToken.None);
|
|
||||||
|
|
||||||
var result = await UserManager.CreateAsync(user, Input.Password);
|
if (firstRender)
|
||||||
if (!result.Succeeded)
|
|
||||||
{
|
{
|
||||||
NotificationService.Notify(new NotificationMessage()
|
if (!string.IsNullOrEmpty(Errors))
|
||||||
{
|
{
|
||||||
Severity = NotificationSeverity.Error,
|
NotificationService.Notify(new NotificationMessage()
|
||||||
Summary = "Ошибка",
|
{
|
||||||
Detail = string.Join(", ", result.Errors.Select(error => error.Description))
|
Severity = NotificationSeverity.Error,
|
||||||
});
|
Summary = "Ошибка",
|
||||||
return;
|
Detail = Errors,
|
||||||
|
Duration = -1d
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await SignInManager.SignInAsync(user, isPersistent: false);
|
|
||||||
|
|
||||||
RedirectManager.RedirectTo(ReturnUrl);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
@page "/test/export"
|
@page "/test/export"
|
||||||
|
|
||||||
@using Microsoft.AspNetCore.Authorization
|
|
||||||
@using Hcs.Broker
|
@using Hcs.Broker
|
||||||
@using Hcs.Broker.Logger
|
@using Hcs.Broker.Logger
|
||||||
@using Hcs.Broker.MessageCapturer
|
@using Hcs.Broker.MessageCapturer
|
||||||
@using Hcs.Service.Async.Nsi
|
@using Hcs.Service.Async.Nsi
|
||||||
@using Hcs.WebApp.Config
|
@using Hcs.WebApp.Config
|
||||||
@using Hcs.WebApp.Utils
|
@using Hcs.WebApp.Utils
|
||||||
|
@using Microsoft.AspNetCore.Authorization
|
||||||
|
|
||||||
@implements IDisposable
|
@implements IDisposable
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
@using System.Net.Http
|
@using System.Net.Http
|
||||||
@using System.Net.Http.Json
|
@using System.Net.Http.Json
|
||||||
|
@using Hcs.WebApp
|
||||||
|
@using Hcs.WebApp.Components
|
||||||
|
@using Hcs.WebApp.Components.Shared
|
||||||
@using Microsoft.AspNetCore.Components.Authorization
|
@using Microsoft.AspNetCore.Components.Authorization
|
||||||
@using Microsoft.AspNetCore.Components.Forms
|
@using Microsoft.AspNetCore.Components.Forms
|
||||||
@using Microsoft.AspNetCore.Components.Routing
|
@using Microsoft.AspNetCore.Components.Routing
|
||||||
@ -8,8 +11,5 @@
|
|||||||
@using Microsoft.JSInterop
|
@using Microsoft.JSInterop
|
||||||
@using Radzen
|
@using Radzen
|
||||||
@using Radzen.Blazor
|
@using Radzen.Blazor
|
||||||
@using Hcs.WebApp
|
|
||||||
@using Hcs.WebApp.Components
|
|
||||||
@using Hcs.WebApp.Components.Shared
|
|
||||||
|
|
||||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||||
|
|||||||
55
Hcs.WebApp/Controllers/IdentityController.cs
Normal file
55
Hcs.WebApp/Controllers/IdentityController.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using Hcs.WebApp.Data;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Radzen;
|
||||||
|
|
||||||
|
namespace Hcs.WebApp.Controllers
|
||||||
|
{
|
||||||
|
[Route("identity/[action]")]
|
||||||
|
public class IdentityController(
|
||||||
|
IUserStore<AppUser> userStore,
|
||||||
|
UserManager<AppUser> userManager,
|
||||||
|
SignInManager<AppUser> signInManager) : Controller
|
||||||
|
{
|
||||||
|
private readonly IUserStore<AppUser> userStore = userStore;
|
||||||
|
private readonly UserManager<AppUser> userManager = userManager;
|
||||||
|
private readonly SignInManager<AppUser> signInManager = signInManager;
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Register(string userName, string password, string returnUrl)
|
||||||
|
{
|
||||||
|
var user = Activator.CreateInstance<AppUser>();
|
||||||
|
await userStore.SetUserNameAsync(user, userName, CancellationToken.None);
|
||||||
|
|
||||||
|
var result = await userManager.CreateAsync(user, password);
|
||||||
|
if (!result.Succeeded)
|
||||||
|
{
|
||||||
|
var errors = string.Join(", ", result.Errors.Select(error => error.Description));
|
||||||
|
if (!string.IsNullOrEmpty(returnUrl))
|
||||||
|
{
|
||||||
|
return Redirect($"/account/register?errors={errors}&returnUrl={Uri.EscapeDataString(returnUrl)}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Redirect($"/account/register?errors={errors}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await signInManager.SignInAsync(user, isPersistent: false);
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(returnUrl))
|
||||||
|
{
|
||||||
|
Redirect("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Redirect(returnUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Logout()
|
||||||
|
{
|
||||||
|
await signInManager.SignOutAsync();
|
||||||
|
|
||||||
|
return Redirect("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,5 +3,5 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace Hcs.WebApp.Data
|
namespace Hcs.WebApp.Data
|
||||||
{
|
{
|
||||||
internal class AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : IdentityDbContext<AppUser>(options) { }
|
public class AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : IdentityDbContext<AppUser, AppRole, string>(options) { }
|
||||||
}
|
}
|
||||||
|
|||||||
6
Hcs.WebApp/Data/AppRole.cs
Normal file
6
Hcs.WebApp/Data/AppRole.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
|
namespace Hcs.WebApp.Data
|
||||||
|
{
|
||||||
|
public class AppRole : IdentityRole { }
|
||||||
|
}
|
||||||
@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
namespace Hcs.WebApp.Data
|
namespace Hcs.WebApp.Data
|
||||||
{
|
{
|
||||||
internal class AppUser : IdentityUser { }
|
public class AppUser : IdentityUser { }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
namespace Hcs.WebApp.Data.Migrations
|
namespace Hcs.WebApp.Data.Migrations
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
internal partial class CreateIdentitySchema : Migration
|
public partial class CreateIdentitySchema : Migration
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
|||||||
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
|
|||||||
namespace Hcs.WebApp.Data.Migrations
|
namespace Hcs.WebApp.Data.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(AppIdentityDbContext))]
|
[DbContext(typeof(AppIdentityDbContext))]
|
||||||
internal class AppIdentityDbContextModelSnapshot : ModelSnapshot
|
public class AppIdentityDbContextModelSnapshot : ModelSnapshot
|
||||||
{
|
{
|
||||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.20" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.20" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.HeaderPropagation" Version="8.0.20" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.20" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.20" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.11" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.11" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.20" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.20" />
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
using Hcs.WebApp.Data;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using System.Security.Claims;
|
|
||||||
|
|
||||||
namespace Hcs.WebApp.Identity
|
|
||||||
{
|
|
||||||
internal static class IdentityComponentsEndpointRouteBuilderExtensions
|
|
||||||
{
|
|
||||||
public static IEndpointConventionBuilder MapAdditionalIdentityEndpoints(this IEndpointRouteBuilder endpoints)
|
|
||||||
{
|
|
||||||
ArgumentNullException.ThrowIfNull(endpoints);
|
|
||||||
|
|
||||||
var accountGroup = endpoints.MapGroup("/account");
|
|
||||||
accountGroup.MapPost("/logout", async (
|
|
||||||
ClaimsPrincipal user,
|
|
||||||
SignInManager<AppUser> signInManager,
|
|
||||||
[FromForm] string returnUrl) =>
|
|
||||||
{
|
|
||||||
await signInManager.SignOutAsync();
|
|
||||||
return TypedResults.LocalRedirect($"~/{returnUrl}");
|
|
||||||
});
|
|
||||||
|
|
||||||
return accountGroup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,62 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,45 +0,0 @@
|
|||||||
using Hcs.WebApp.Data;
|
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Components.Server;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Security.Claims;
|
|
||||||
|
|
||||||
namespace Hcs.WebApp.Identity
|
|
||||||
{
|
|
||||||
internal sealed class IdentityRevalidatingAuthenticationStateProvider(
|
|
||||||
ILoggerFactory loggerFactory,
|
|
||||||
IServiceScopeFactory scopeFactory,
|
|
||||||
IOptions<IdentityOptions> options) : RevalidatingServerAuthenticationStateProvider(loggerFactory)
|
|
||||||
{
|
|
||||||
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30);
|
|
||||||
|
|
||||||
protected override async Task<bool> ValidateAuthenticationStateAsync(
|
|
||||||
AuthenticationState authenticationState, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
await using var scope = scopeFactory.CreateAsyncScope();
|
|
||||||
|
|
||||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<AppUser>>();
|
|
||||||
return await ValidateSecurityStampAsync(userManager, authenticationState.User);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<bool> ValidateSecurityStampAsync(UserManager<AppUser> userManager, ClaimsPrincipal principal)
|
|
||||||
{
|
|
||||||
var user = await userManager.GetUserAsync(principal);
|
|
||||||
if (user is null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if (!userManager.SupportsUserSecurityStamp)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var principalStamp = principal.FindFirstValue(options.Value.ClaimsIdentity.SecurityStampClaimType);
|
|
||||||
var userStamp = await userManager.GetSecurityStampAsync(user);
|
|
||||||
return principalStamp == userStamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
using Hcs.WebApp.Data;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
|
|
||||||
namespace Hcs.WebApp.Identity
|
|
||||||
{
|
|
||||||
internal sealed class IdentityUserAccessor(UserManager<AppUser> userManager, IdentityRedirectManager redirectManager)
|
|
||||||
{
|
|
||||||
public async Task<AppUser> GetRequiredUserAsync(HttpContext context)
|
|
||||||
{
|
|
||||||
var user = await userManager.GetUserAsync(context.User);
|
|
||||||
if (user is null)
|
|
||||||
{
|
|
||||||
redirectManager.RedirectToWithStatus("account/invalid_user", $"Ошибка: Не удалось загрузить пользователя с идентификатором '{userManager.GetUserId(context.User)}'", context);
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +1,5 @@
|
|||||||
using Hcs.WebApp.Components;
|
using Hcs.WebApp.Components;
|
||||||
using Hcs.WebApp.Data;
|
using Hcs.WebApp.Data;
|
||||||
using Hcs.WebApp.Identity;
|
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Radzen;
|
using Radzen;
|
||||||
@ -12,52 +10,49 @@ builder.Services
|
|||||||
.AddRazorComponents()
|
.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddRadzenComponents();
|
builder.Services.AddRadzenComponents();
|
||||||
|
|
||||||
builder.Services.AddCascadingAuthenticationState();
|
builder.Services.AddHttpClient("Hcs.WebApp").AddHeaderPropagation(x => x.Headers.Add("Cookie"));
|
||||||
builder.Services.AddScoped<IdentityUserAccessor>();
|
builder.Services.AddHeaderPropagation(x => x.Headers.Add("Cookie"));
|
||||||
builder.Services.AddScoped<IdentityRedirectManager>();
|
builder.Services.AddAuthentication();
|
||||||
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
|
builder.Services.AddAuthorization();
|
||||||
|
|
||||||
builder.Services
|
|
||||||
.AddAuthentication(options =>
|
|
||||||
{
|
|
||||||
options.DefaultScheme = IdentityConstants.ApplicationScheme;
|
|
||||||
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
|
|
||||||
})
|
|
||||||
.AddIdentityCookies();
|
|
||||||
|
|
||||||
var connectionString = builder.Configuration.GetConnectionString("IdentityConnection") ?? throw new InvalidOperationException("<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> 'IdentityConnection'");
|
var connectionString = builder.Configuration.GetConnectionString("IdentityConnection") ?? throw new InvalidOperationException("<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> 'IdentityConnection'");
|
||||||
builder.Services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(connectionString));
|
builder.Services.AddDbContext<AppIdentityDbContext>(options => options.UseSqlServer(connectionString));
|
||||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
|
||||||
|
|
||||||
builder.Services
|
builder.Services
|
||||||
.AddIdentityCore<AppUser>()
|
.AddIdentity<AppUser, AppRole>(options =>
|
||||||
|
{
|
||||||
|
options.Password.RequiredLength = 6;
|
||||||
|
options.Password.RequireNonAlphanumeric = false;
|
||||||
|
options.Password.RequireDigit = false;
|
||||||
|
options.Password.RequireLowercase = false;
|
||||||
|
options.Password.RequireUppercase = false;
|
||||||
|
options.Password.RequiredUniqueChars = 1;
|
||||||
|
})
|
||||||
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
||||||
.AddSignInManager()
|
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (!app.Environment.IsDevelopment())
|
||||||
{
|
|
||||||
app.UseMigrationsEndPoint();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
app.UseExceptionHandler("/error", createScopeForErrors: true);
|
app.UseExceptionHandler("/error", createScopeForErrors: true);
|
||||||
app.UseHsts();
|
app.UseHsts();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
app.UseHeaderPropagation();
|
||||||
|
app.UseRouting();
|
||||||
app.UseAntiforgery();
|
app.UseAntiforgery();
|
||||||
|
app.UseAuthentication();
|
||||||
|
app.UseAuthorization();
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
app
|
app
|
||||||
.MapRazorComponents<App>()
|
.MapRazorComponents<App>()
|
||||||
.AddInteractiveServerRenderMode();
|
.AddInteractiveServerRenderMode();
|
||||||
|
|
||||||
app.MapAdditionalIdentityEndpoints();
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user