Curtis Mullett Curtis Mullett - 1 month ago 9
ASP.NET (C#) Question

Account Register on ASP net core not registering user

Hi I'm trying to make a website for my company and I'm trying to get user accounts to work however everytime I try to register a new user on the register page it does not create a user in the database. I have confirmed this multiple times by looking in the SSOX localDB. The website is written in asp dotnet core. The only modifications I have done to this after the authentication template is added a user column using Entity Framework code first, and have added the user input into forms and the controllers accordingly. I have also followed the tutorial on https://docs.asp.net/en/latest/security/authentication/accconfirm.html
to add account confirmation.

As dotnetcore asp is fairly new there is not many questions asked and I have searched for my problem on google and also on stack but have failed to find an answer to my problem. I have included my code for the Account Controller, Register View Model and Register View below.

If anybody knows of a solution I would be very greatful.

Account Controller Register code:

// GET: /Account/Register
[HttpGet]
[AllowAnonymous]
public IActionResult Register(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}

//
// POST: /Account/Register
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{

// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(model.UserName, model.Email, "Confirm your account", $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
//Line commented to prevent user logging in without confirming account. //await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, $"User {user.Id} created a new account");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}

// If we got this far, something failed, redisplay form
return View(model);
}


Here is my RegisterViewModel Code:

using System.ComponentModel.DataAnnotations;

namespace MainSite.Models.AccountViewModels
{
public class RegisterViewModel
{
[Required]
[Display(Name = "Username")]
public string UserName { get; set; }

[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}


And my Register View code:

@model RegisterViewModel
@{
ViewData["Title"] = "Register";
}

<h2>@ViewData["Title"].</h2>

<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<h4>Create a new account.</h4>
<hr />
<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="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" 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">
<label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Register</button>
</div>
</div>
</form>

@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

Answer

Replace the [Authorize] attribute with the [AllowAnonymous] one.

Comments