using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using ApprovementWorkflowSample.Applications;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
namespace ApprovementWorkflowSample.Views
{
public partial class SignIn
{
[Inject]
public IJSRuntime? JSRuntime { get; init; }
[Inject]
public NavigationManager? Navigation { get; init; }
[Inject]
public IApplicationUserService? ApplicationUsers{get; init; }
[Inject]
public SignInManager<ApplicationUser>? SignInManager { get; init; }
[Inject]
public IHostEnvironmentAuthenticationStateProvider? HostAuthentication { get; init; }
[Inject]
public AuthenticationStateProvider? AuthenticationStateProvider{get; init; }
...
public async Task StartSigningIn()
{
...
ApplicationUser? user = await ApplicationUsers!.GetUserByEmailAsync(Email);
if(user == null)
{
await HandleSigningInFailedAsync("Email or Password are not match");
return;
}
SignInResult loginResult = await SignInManager!.CheckPasswordSignInAsync(user, Password, false);
if(loginResult.Succeeded == false)
{
await HandleSigningInFailedAsync("Email or Password are not match");
return;
}
if(loginResult.Succeeded)
{
ClaimsPrincipal principal = await SignInManager.CreateUserPrincipalAsync(user);
SignInManager.Context.User = principal;
HostAuthentication!.SetAuthenticationState(
Task.FromResult(new AuthenticationState(principal)));
// If you don't need doing anything without moving to next page, you can remove this.
AuthenticationState authState = await AuthenticationStateProvider!.GetAuthenticationStateAsync();
Navigation!.NavigateTo("/Pages/Edit");
}
}