@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }
[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();
private string? errorMessage;
private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");
}
}
@code {
[CascadingParameter]
public HttpContext httpContext { get; set; }
[SupplyParameterFromForm]
public LoginViewModel Model { get; set; } = new();
private string? errorMessage;
private async Task Authenticate()
{
var userAccount = appDbContext.UserAccounts.Where(x => x.UserName == Model.UserName).FirstOrDefault();
if(userAccount is null || userAccount.Password != Model.Password)
{
errorMessage = "Invalid User Name or Password";
return;
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, Model.UserName),
new Claim(ClaimTypes.Role, userAccount.Role)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
await httpContext.SignInAsync(principal);
navigationManager.NavigateTo("/");
}
}