使用Int Key自定义Asp.net标识

时间:2015-06-22 19:59:18

标签: c# asp.net sql-server asp.net-mvc entity-framework

我正在尝试使用Int作为主键自定义Asp.net标识。我已经实现了将string密钥更改为int密钥的代码。

using Microsoft.AspNet.Identity.EntityFramework;

namespace NewAsp.netAuth.Models
{
    public class ApplicationUser :  IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
    }

    public class CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
    int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }
}  

此外,我希望用户使用他们的电子邮件地址登录我的网站而不是用户名,因此我更改AccountViewModels.cs。通过注册,输入的电子邮件地址将保存在Username列,而不是Email列。

这是我的代码:

public class LoginViewModel
{
        [Required]
        [Display(Name = "EMail Adress")]
        [EmailAddress]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "Email Adress")]
        [EmailAddress]
        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 AccountController(UserManager<ApplicationUser,int> userManager)
{
            UserManager = userManager;
            var userValidator = UserManager.UserValidator as UserValidator<ApplicationUser,int>;
            userValidator.AllowOnlyAlphanumericUserNames = false;
        }

在这种情况下,我有一些问题:

  1. 如何删除EmailEmailConfirmed, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEndDateUtc,和...列。
  2. 如何添加一些新列,例如Name, Family,...
  3. 如何完全删除AspNetUserClaims
  4. 是否可以删除AspNetUserRoles表并在AspNetUsers表中添加来自AspNetRoles表的外键?
  5. 我阅读了很多教程,但找不到任何好的解决方案

1 个答案:

答案 0 :(得分:0)

您需要替换默认数据访问实施。

首先,您创建自己的User类。它将包含整数主键,电子邮件作为用户名以及您想要的任何自定义字段。

class User : IUser<int>
{
   public int Id
   { get; set; }

   public string Name
   { get; set; }

   public string Email
   { get; set; }

   public string PasswordHash
   { get; set; }

   public string SecurityStamp
   { get; set; }

   // other fields here

   string IUser<int>.UserName
   {
      get { return Email; }
      set { Email = value; }
   }
}

然后实现UserStore接口。您可能还需要实现其他接口。

class CustomUserStore: 
    IUserStore<User, int>,
    IUserPasswordStore<User, int>,
    IUserSecurityStampStore<User, int>
{
    AppDbContext context = new AppDbContext();

    public Task<User> FindByIdAsync(int userId)
    {
        return context.Users.FindAsync(userId);
    }

    public Task<User> FindByNameAsync(string userName)
    {
        return context.Users.FirstOrDefaultAsync(u => u.Email == userName);
    }

    public Task CreateAsync(User user)
    {
        context.Entry(user).State = EntityState.Added;
        return context.SaveChangesAsync();
    }

    public Task UpdateAsync(User user)
    {
        context.Entry(user).State = EntityState.Modified;
        return context.SaveChangesAsync();
    }

    public Task DeleteAsync(User user)
    {
        context.Entry(user).State = EntityState.Deleted;
        return context.SaveChangesAsync();
    }

    public Task<bool> HasPasswordAsync(User user)
    {
        return Task.FromResult(user.PasswordHash != null);
    }

    public Task<string> GetPasswordHashAsync(User user)
    {
        return Task.FromResult(user.PasswordHash);
    }

    public Task SetPasswordHashAsync(User user, string passwordHash)
    {
        user.PasswordHash = passwordHash;
        return Task.FromResult(0);
    }

    public Task<string> GetSecurityStampAsync(User user)
    {
        return Task.FromResult(user.SecurityStamp);
    }

    public Task SetSecurityStampAsync(User user, string stamp)
    {
        user.SecurityStamp = stamp;
        return Task.FromResult(0);
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

将新的UserStore实现传递给UserManager:

class CustomUserManager : UserManager<User, int>
{
    public CustomUserManager()
        : base(new CustomUserStore())
    {

    }
}

这将帮助您入门。