简体   繁体   English

使用单个用户帐户时如何在AspNetUsers表中设置PhoneNumber

[英]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 我还编辑了Re​​gisterBindingModedl以合并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.

相关问题 使用存储过程从 AspNetUsers 表中检索所有或单个用户 - Using a stored procedure to retrieve all or single user from AspNetUsers table Blazor WebAssembly 应用程序具有个人用户帐户和应用程序内存储用户帐户使用不提供本地用户帐户 - Blazor WebAssembly App with Individual User Accounts and Store user accounts in-app using gives no local user accounts 在 AspNetUsers 表中使用 MVC 身份框架时如何在数据库中插入值? - How to insert a value in Database when using MVC identity framework in AspNetUsers Table? 使用ASP.NET Web Api时如何检查电话号码在AspNetUsers表中是否唯一 - How to check phone number is unique in AspNetUsers table when using ASP.NET Web Api 如何从WebAPI更新AspNetUsers表中现有用户记录的密码 - How to update password for the existing user record in AspNetUsers table from WebAPI 如何在MVC与2017中设置个人用户帐户和启用迁移 - how set individual user accounts and enable-migrations in mvc vs 2017 如何在AspNetUsers表中保存字段 - How to save a field in the AspNetUsers table 在.NET Core中同时使用Active Directory身份验证和单个用户帐户 - Using Both Active Directory Authentication and Individual User Accounts in .NET Core 使用单个用户帐户的Azure移动服务.net后端身份验证 - Azure Mobile Services .net Backend Authentication using individual user accounts 在我的ASPNETUsers表中创建“假”用户 - Create “fake” user in my ASPNETUsers Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM