0

所以我最近一直在玩 MVC 5,我喜欢它。我遇到了一些我认为很容易做到的事情,但最终却在我一天中的几个小时里吃掉了,我一点也不聪明。

基本上我想通过添加我自己的属性来扩展用户模型。我这样做了:

public class IdentityUser : User
{
    public IdentityUser()
    //: this(String.Empty)
    {
    }

    public IdentityUser(string userName)
        : base(userName)
    {
    }

    public string UserId { get; set; }
    public string CompanyId { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredTitle")]
    public string Title { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredFirstName")]
    public string Forename { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredLastName")]
    public string Surname { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredUserName")]
    public string UserName { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredEmail")]
    public string Email { get; set; }
    public string JobTitle { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string Photo { get; set; }
    public string LinkedIn { get; set; }
    public string Twitter { get; set; }
    public string Facebook { get; set; }
    public string Google { get; set; }
    public string Bio { get; set; }

    public string CompanyName { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredCredentialId")]
    public string CredentialId { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredSecurityCode")]
    public string Code { get; set; }
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }

    public DateTime LastLoginDate { get; set; }

    [Display(Name = "Can only edit own assets")]
    public bool UserCanEditOwn { get; set; }
    [Display(Name = "Can edit assets")]
    public bool UserCanEdit { get; set; }
    [Display(Name = "Can download assets")]
    public bool UserCanDownload { get; set; }
    [Display(Name = "Require approval to upload assets")]
    public bool UserRequiresApproval { get; set; }
    [Display(Name = "Can approve assets")]
    public bool UserCanApprove { get; set; }
    [Display(Name = "Can synchronise assets")]
    public bool UserCanSync { get; set; }

    public bool AgreedTerms { get; set; }
}

public class IdentityUserContext : IdentityStoreContext
{
    public IdentityUserContext(DbContext db)
        : base(db)
    {
        this.Users = new UserStore<IdentityUser>(this.DbContext);
    }
}

public class IdentityUserDbContext : IdentityDbContext<IdentityUser, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}

User是 EF 中的默认 User 类

namespace Microsoft.AspNet.Identity.EntityFramework
{
    public class User : IUser
    {
    public User();
    public User(string userName);

    [Key]
    public string Id { get; set; }
    public string UserName { get; set; }
    }
}

我继承了它,因为我在某处读到,如果你想使用自定义类,你必须这样做。无论如何,然后我将我的 Register 模型更改为如下所示:

public class RegisterViewModel
{
    [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; }

    public string Title { get; set; }
    [Required]
    public string Forename { get; set; }
    [Required]
    public string Surname { get; set; }
    [Required]
    public string Email { get; set; }

    public string CompanyName { get; set; }

    [Required]
    public string CredentialId { get; set; }
    [Required]
    public string Code { get; set; }
}

以及看起来像这样的方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        try
        {
            // Create a profile, password, and link the local login before signing in the user
            var companyId = Guid.NewGuid().ToString();
            var user = new IdentityUser( model.UserName)
            {
                CompanyId = companyId,
                Title = model.Title,
                Forename = model.Forename,
                Surname = model.Surname,
                Email = model.Email,
                CompanyName = model.CompanyName,
                CredentialId = model.CredentialId
            };

            if (await IdentityStore.CreateLocalUser(user, model.Password))
            {
                await AuthenticationManager.SignIn(HttpContext, user.Id, isPersistent: false);
                return RedirectToAction("Setup", new { id = user.CompanyId });
            }
            else
            {
                ModelState.AddModelError("", "Failed to register user name: " + model.UserName);
            }
        }
        catch (IdentityException e)
        {
            ModelState.AddModelError("", e.Message);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

当我尝试注册时,我收到此错误:

Mapping and metadata information could not be found for EntityType 'TestProject.Models.IdentityUser'.

我不知道为什么。

我错过了一步还是什么?

任何帮助将不胜感激

4

3 回答 3

1

您可以合并 IdentityUser 和 User。只要它继承自 IUser,你应该没问题。

也许从那开始。

编辑:

这是我当前使用并使用 EF Code First 存储它的用户类。

public class User : IUser
{
    [Key]
    public string Id { get; set;}

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

    [Required]
    [StringLength(255, MinimumLength = 5)]
    public string UserName { get; set; }
}
于 2013-11-13T19:46:17.203 回答
0

加个演员怎么样?

IdentityStore.CreateLocalUser(user as User, model.Password)
于 2013-11-14T02:55:10.437 回答
0

我想通了感谢这篇文章:

Microsoft.AspNet.Identity 的自定义成员身份 - CreateLocalUser 失败

我的班级现在看起来像这样:

public class User : IUser
{
    public User()
    {
        this.LastLoginDate = DateTime.UtcNow;
        this.DateCreated = DateTime.UtcNow;
    }

    public User(string userName)
    {
        this.Id = Guid.NewGuid().ToString();
        this.UserName = userName;

        this.CreatedBy = this.Id;

        this.LastLoginDate = DateTime.UtcNow;
        this.DateCreated = DateTime.UtcNow;

        this.IsApproved = true;
    }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredUserName")]
    public string UserName { get; set; }

    public string Id { get; set; }
    [Required] public string CompanyId { get; set; }

    [Required] public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }

    public DateTime DateCreated { get; set; }
    public DateTime? DateModified { get; set; }
    public DateTime LastLoginDate { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredTitle")]
    public string Title { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredFirstName")]
    public string Forename { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredLastName")]
    public string Surname { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredEmail")]
    public string Email { get; set; }
    public string JobTitle { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string Photo { get; set; }
    public string LinkedIn { get; set; }
    public string Twitter { get; set; }
    public string Facebook { get; set; }
    public string Google { get; set; }
    public string Bio { get; set; }

    public string CompanyName { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredCredentialId")]
    public string CredentialId { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredSecurityCode")]
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }

    [Display(Name = "Can only edit own assets")]
    public bool CanEditOwn { get; set; }
    [Display(Name = "Can edit assets")]
    public bool CanEdit { get; set; }
    [Display(Name = "Can download assets")]
    public bool CanDownload { get; set; }
    [Display(Name = "Require approval to upload assets")]
    public bool RequiresApproval { get; set; }
    [Display(Name = "Can approve assets")]
    public bool CanApprove { get; set; }
    [Display(Name = "Can synchronise assets")]
    public bool CanSync { get; set; }

    public bool AgreedTerms { get; set; }
    public bool Deleted { get; set; }
}

public class UserContext : IdentityStoreContext
{
    public UserContext(DbContext db)
        : base(db)
    {
        this.Users = new UserStore<User>(this.DbContext);
    }
}

public class UserDbContext : IdentityDbContext<User, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}

在我的 AccountController 构造函数中,我有这个:

    public AccountController() 
    {
        IdentityStore = new IdentityStoreManager(new UserContext(new UserDbContext()));
        AuthenticationManager = new IdentityAuthenticationManager(IdentityStore);
    }

就是这样,一切都按预期工作:)

于 2013-11-14T11:47:55.607 回答