[英]How to set PhoneNumber in AspNetUsers table when using individual user accounts
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. 我创建了一个新的Asp.Net Web API项目,并使用了身份验证类型“个人用户帐户”,该类型创建了带有一些默认表的数据库。 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. 当用户注册时,我希望他们输入一个电子邮件地址,一个密码和一个电话号码,它们应该将相应的值添加到AspNetUsers表中的相应列。
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 到目前为止,我已经在帐户控制器中编辑了注册方法以提交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 我还编辑了RegisterBindingModedl以合并PhoneNumber属性
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. 用户被提交到数据库,数据库给出200响应并提交。 When I view the database in the database, the user appears but the PhoneNumber is null. 当我在数据库中查看数据库时,出现用户,但PhoneNumber为null。
How can I add a user with a PhoneNumber? 如何添加带有电话号码的用户?
创建用户后,在您的Register post方法中尝试调用
await _usermanager.SetPhoneNumberAsync(user, model.PhoneNumber)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.