5
I have a Web API project and I’m using Identity to manage user accounts, my controller is like this:
public async Task<IHttpActionResult> Register([FromBody]RegisterDto model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
var registerDomain = Mapper.Map<RegisterDto, Customer>(model);
registerDomain.UserName = registerDomain.Email;
_customerService.Insert(registerDomain);
IdentityResult result = await UserManager.CreateAsync(registerDomain, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
var code = await UserManager.GenerateEmailConfirmationTokenAsync(registerDomain.Id);
var callbackUrl = Url.Link("DefaultApi", new { controller = "Account", action = "ConfirmEmail", userId = registerDomain.Id, code = code });
await UserManager.SendEmailAsync(registerDomain.Id, "Confirmação de conta", "Porfavor confirme a sua conta clicando nesse link: <a href=\"" + callbackUrl + "\">link</a>");
}
catch (Exception ex)
{
return BadRequest();
}
return Ok();
}
But the result of UserManager.CreateAsync is making the following mistake: Name cannot be null or empty.
Name is a property of Customer (Customer inherits from IdentityUser) and the string is being passed on correctly, including in RegisterDto the property is required, if it was null or empty it did not pass the ModelState.
I’ve searched the net and there are people with the same problem, but the solutions are not working, someone can give a tip?
The code of RegisterDto
public class RegisterDto
{
public string Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Cpf { get; set; }
[Required]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "A senha e a confirmação da senha não são iguais.")]
public string ConfirmPassword { get; set; }
}
I forgot to mention, I believe it has nothing to do directly with the property Name of Customer, I say this because in the answers I found on the net the custom class of Identityuser had no property called Name, really I’m lost here. :(
– rhgm
Can you please edit your question and enter the code for
RegisterDto?– Leonel Sanches da Silva
@I added the code to the
RegisterDto.– rhgm