I have created a new Asp.Net Web API project and used the authentication type "Individual User Accounts" which created a database with some default tables. I then created a number of accounts with emails and passwords successfully. When the user is registering, I want them to input an email address, a password and a phone number which should add the corresponding values to the corresponding columns in the AspNetUsers table.
I have successfully managed to add the email address and password but how can I add the phone number?
So far, I in the account controller I have edited the register method to submit the PhoneNumber
// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
I have also edited the RegisterBindingModedl to incorporate the PhoneNumber property
public class RegisterBindingModel
{
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} 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; }
[Required]
[StringLength(11, ErrorMessage = "The telephone number must be 11 characters long.", MinimumLength = 11)]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
}
When I submit the phone number following using postman
{
"Email": "test@gmail.com",
"Password": ".Password123.",
"ConfirmPassword": ".Password123.",
"PhoneNumber": "01642333333"
}
The user is submitted to the database it gives a 200 response and submits. When I view the database in the database, the user appears but the PhoneNumber is null.
How can I add a user with a PhoneNumber?
创建用户后,在您的Register post方法中尝试调用
await _usermanager.SetPhoneNumberAsync(user, model.PhoneNumber)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.