Log in system error
I was trying to make a log in system for my web app, but my code doesn't seem to work
using AplikacjaWeb.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace AplikacjaWeb.Pages.Registration
{
public class RegistrationModel : PageModel
{
private readonly UserManager<UserModel> _userManager;
private readonly SignInManager<UserModel> _signInManager;
public RegistrationModel(UserManager<UserModel> userManager, SignInManager<UserModel> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
[BindProperty]
public Rejestracja /*Registration in english*/ Input { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
var user = new UserModel { UserName = Input.Username, Email = Input.Email, PasswordHash = Input.Password };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToPage("/Index");
}
else
{
Console.WriteLine("Error");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
return RedirectToPage("/Index");
}
}
}
9 Replies
am I missing something?
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
"doesn't work" isn't specific
when the user enters their data into the registration form, it's not being sent to the database
more specifically, when I hit the "submit" button, console prints out "error", which is what I did here
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToPage("/Index");
}
else
{
Console.WriteLine("Error");
}
so it's basically !result.Succeededwhat does it mean if the result of
CreateAsync
isn't successful? do the docs say anything?I mean it basically says that something went wrong trying to register
also what docs do you mean?
the documentation for identity that you're presumably following to implement this
Ummmmmmm idk what you're talking about