Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.AspNetCore.Identity.IdentityError when attempting registration

I'm implementing a simple authentication system with ASP.NET Core Identity using only username and password. When I call Register action, I get the error mentioned in the title.

Actual error

Below the code which is associated with registration.

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
   services.AddDbContext<AppDbContext>(options => 
   options.UseSqlServer(_configuration.GetConnectionString("Default")));
   services.AddIdentity<IdentityUser, 
   IdentityRole().AddEntityFrameworkStores<AppDbContext>();`

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseAuthentication();
}

AccountController.cs:

public class AccountController : Controller
{
    private readonly UserManager<IdentityUser> _userManager;
    private readonly SignInManager<IdentityUser> _signInManager;

    public AccountController(UserManager<IdentityUser> userManager, 
    SignInManager<IdentityUser> signInManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
    }
    public IActionResult Register()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public async Task<IActionResult> Register(RegistrationViewModel registrationViewModel)
    {
        if (ModelState.IsValid)
        {
            var user = new IdentityUser {UserName = registrationViewModel.UserName};
            var result = await _userManager.CreateAsync(user, registrationViewModel.Password);

            if (result.Succeeded)
                return RedirectToAction("Index", "Home");
            var errors = result.Errors;
            var message = string.Join(", ", errors);
            ModelState.AddModelError("", message);
        }

        return View(registrationViewModel);
    }

RegistrationViewModel.cs:

public class RegistrationViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    
    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

Registerm.cshtml:

@model RegistrationViewModel

<form asp-controller="Account" asp-action="Register" method="post" class="form-horizontal" role="form">

<div asp-validation-summary="All" class="text-danger"></div>

<div class="form-group">
    <label asp-for="UserName" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <input asp-for="UserName" class="form-control" />
        <span asp-validation-for="UserName" class="text-danger"></span>
    </div>
</div>
<div class="form-group">
    <label asp-for="Password" class="col-md-2 control-label"></label>
    <div class="col-md-10">
        <input asp-for="Password" class="form-control" />
        <span asp-validation-for="Password" class="text-danger"></span>
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-primary" value="Register" />
    </div>
</div>
`

As far as I know, such error occurs when the login is already associated with account, however, the table which is supposed to store all the users' data is empty.

like image 331
John Nash Avatar asked May 29 '26 19:05

John Nash


1 Answers

Try using this to get a better error message. Your toString on Errors is just returning the objects type not whats in it.

var message =  string.Join(", ", response.Errors.Select(x => "Code " + x.Code + " Description" + x.Description));
like image 183
Ryan Schlueter Avatar answered May 31 '26 08:05

Ryan Schlueter