C
C#12mo ago
CrosRoad95

how to use SignInManager.SignInAsync in blazor web app?

.net8, using blazor webapp, created form using mudBlazor:
<EditForm Model="@_model" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator />
<ForumAlert AlertModel="_alert" />
<MudCardContent>
<MudTextField Label="First name" HelperText="Max. 8 characters"
@bind-Value="_model.UserName" For="@(() => _model.UserName)" />
<MudTextField Label="Password" HelperText="Choose a strong password" Class="mt-3"
@bind-Value="_model.Password" For="@(() => _model.Password)" InputType="InputType.Password" />
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Zaloguj się</MudButton>
</MudCardActions>
</EditForm>

@code{
private async Task OnValidSubmit(EditContext context)
{
var model = (LoginFormModel)context.Model;
var user = await _userManager.FindByNameAsync(model.UserName);
if (user != null)
{
//var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic"));
//await _httpContextAccessor.HttpContext.SignInAsync(principal);
await _signInManager.SignInAsync(user, true);
_navigationManager.NavigateTo("/");
}
else
{
_alert.Warning("User not found");
}
}
}
<EditForm Model="@_model" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator />
<ForumAlert AlertModel="_alert" />
<MudCardContent>
<MudTextField Label="First name" HelperText="Max. 8 characters"
@bind-Value="_model.UserName" For="@(() => _model.UserName)" />
<MudTextField Label="Password" HelperText="Choose a strong password" Class="mt-3"
@bind-Value="_model.Password" For="@(() => _model.Password)" InputType="InputType.Password" />
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Zaloguj się</MudButton>
</MudCardActions>
</EditForm>

@code{
private async Task OnValidSubmit(EditContext context)
{
var model = (LoginFormModel)context.Model;
var user = await _userManager.FindByNameAsync(model.UserName);
if (user != null)
{
//var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic"));
//await _httpContextAccessor.HttpContext.SignInAsync(principal);
await _signInManager.SignInAsync(user, true);
_navigationManager.NavigateTo("/");
}
else
{
_alert.Warning("User not found");
}
}
}
but it throws exception: System.InvalidOperationException: 'OnStarting cannot be set because the response has already started.' how can i do this? i'm using cookie authentication
No description
2 Replies
CrosRoad95
CrosRoad95OP12mo ago
help?
Rory&
Rory&12mo ago
oh god cookie auth anyways what even is OnStarting?
Want results from more Discord servers?
Add your server