i make webpage by blazor i have problem to login and i use this normal code for login
protected async Task LoginRegisterUser()
{
try
{
//if (Context.Response.HasStarted== false)
//{
// await _next.Invoke(context);
//}
bool returnData = await CheckActiveCode(RegisterViewItem.PhoneNumber, RegisterViewItem.ActiveCode);
if (returnData)//کد درست بود
{
var usersItem = await userManagerService.SelectUserByPhoneNumberAsync(RegisterViewItem.PhoneNumber);
if (usersItem == null || usersItem.Id <= 0)
{
blazoredToast.ShowError("خطا در ورود کاربر");
return;
}
var validatr = await signInManagerService.CheckPasswordSignInAsync(usersItem, usersItem.PhoneNumber, false);
if (validatr.Succeeded)
{
await signInManagerService.SignInAsync(usersItem, RegisterViewItem.RememberMe);
blazoredToast.ShowSuccess("کاربر در حال ورود", "خوش امدید");
}
else
{
blazoredToast.ShowError("رمز ورود کاربر");
return;
}
}
else//کد اشتباه است
{
blazoredToast.ShowError("کد تایید اشتباه است");
}
}
catch (Exception ex)
{
blazoredToast.ShowError(ex.Message);
}
}
i know this code is true but blazor can not manage Request HttpContext.
how can i use Invoke in this function ?
i use PasswordSignInAsync but not fix this problem and show again this error 'Headers are read-only, response has already started.'
CodePudding user response:
If you are using blazor server you need to create a razor page to login and logout because in blazor you must not access HttpContext (Microsoft recommandation)
You have a sample here for logout but you can make the same for login.
You need just create a razor page and redirect from blazor to this page with _navigationManager.NavigateTo("you razor page", true);
in the razor page add the code :
public async Task OnGetAsync()
{
await _SignInManager.SignInAsync(new ApplicationUser(){ UserName = "[email protected]" }, true);
Response.Redirect("/");
}
You can also create a new razor project with authentication to show how it make it.
CodePudding user response:
The official document has the explaination: SignInManager and UserManager aren't supported in Razor components. Blazor Server apps use ASP.NET Core Identity.
The document: https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-5.0
If you have futher issue,you could upload all of the codes concerned so that i could test for you