子类化IdentityUser ASP.NET

时间:2013-12-13 13:59:14

标签: asp.net authentication asp.net-web-api subclassing

我有一个Web API,我希望使用AutoHubUser而不是默认的(IdentityUser)。但是当我尝试保存它时,我一直收到内部服务器错误。需要改变什么?

public class AutoHubUser : IdentityUser
{
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Birth Date")]
    public DateTime BirthDate { get; set; }

    [Required]
    public Gender Gender { get; set; }

    public virtual ICollection<Auto> Autos { get; set; }
    public virtual ICollection<UserMilestone> UserMilestones { get; set; }
    public virtual ICollection<Friendship> Friendships { get; set; }
}

帐户控制器

    // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        IdentityUser user = new AutoHubUser
        {
            UserName = model.UserName,
            Email = model.Email,
            BirthDate = model.BirthDate,
            Gender = model.Gender
        };

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);
        //IdentityResult result = await new UserManager<AutoHubUser>().CreateAsync(user, model.Password);
        IHttpActionResult errorResult = GetErrorResult(result);

        if (errorResult != null)
        {
            return errorResult;
        }

        return Ok();
    }

我还修改了RegisterBindingModel

public class RegisterBindingModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { 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]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }

    [Required]
    public Gender Gender { get; set; }
}

还需要修改什么?它必须位于UserManager的CreateAsync方法中。但我无法在网上找到一个例子。谢谢!

0 个答案:

没有答案