kri*_*per 3 c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity
我有一个没有注册的项目.管理员在admin中注册用户.该项目没有角色,我只有一种类型的用户.我不需要"AspNetRoles","AspNetUserClaims","AspNewUserLogins","AspNetUserRoles".在"AspNetUsers"表中我只需要"Id","Email","Password"和一些自定义属性.在mvc 5中实现这个的最佳方法是什么?
要向AspNetUsers添加更多列/字段,您需要在Identity Model中使用-update database命令添加数据迁移
您还可以通过覆盖控制键和表名称,如下所示
 protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");
    }
Run Code Online (Sandbox Code Playgroud)
当您使用ASPNET Schema进行用户注册时,我认为您不能避免声明,角色和其他表,但您可以忽略这些.
为了避免ASPNET成员资格中的角色和声明
首先创建一个MVC 5应用程序.然后实现IUser,
public class ApplicationUser : IUser
{
    public ApplicationUser()
    {
        this.Id = Guid.NewGuid().ToString();
    }
    public ApplicationUser(string userName): this()
    {
        UserName = userName;
    }
    public virtual string Id { get; set; }
    public virtual string PasswordHash { get; set; }
    public virtual string SecurityStamp { get; set; }
    public virtual string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
接下来我们需要一个DbContet来存储用户,
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public virtual IDbSet<ApplicationUser> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我们需要实现IUserStore,IUserPasswordStore和IUserSecurityStampStore,
public class MyUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserSecurityStampStore<ApplicationUser>
{
    UserStore<IdentityUser> userStore = new UserStore<IdentityUser>(new ApplicationDbContext());
    public MyUserStore()
    {
    }
    public Task CreateAsync(ApplicationUser user)
    {
        var context = userStore.Context as ApplicationDbContext;
        context.Users.Add(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }
    public Task DeleteAsync(ApplicationUser user)
    {
        var context = userStore.Context as ApplicationDbContext;
        context.Users.Remove(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }
    public Task<ApplicationUser> FindByIdAsync(string userId)
    {
        var context = userStore.Context as ApplicationDbContext;
        return context.Users.Where(u => u.Id.ToLower() == userId.ToLower()).FirstOrDefaultAsync();
    }
    public Task<ApplicationUser> FindByNameAsync(string userName)
    {
        var context = userStore.Context as ApplicationDbContext;
        return context.Users.Where(u => u.UserName.ToLower() == userName.ToLower()).FirstOrDefaultAsync();
    }
    public Task UpdateAsync(ApplicationUser user)
    {
        var context = userStore.Context as ApplicationDbContext;
        context.Users.Attach(user);
        context.Entry(user).State = EntityState.Modified;
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }
    public void Dispose()
    {
        userStore.Dispose();
    }
    public Task<string> GetPasswordHashAsync(ApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.GetPasswordHashAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }
    public Task<bool> HasPasswordAsync(ApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.HasPasswordAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }
    public Task SetPasswordHashAsync(ApplicationUser user, string passwordHash)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.SetPasswordHashAsync(identityUser, passwordHash);
        SetApplicationUser(user, identityUser);
        return task;
    }
    public Task<string> GetSecurityStampAsync(ApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.GetSecurityStampAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }
    public Task SetSecurityStampAsync(ApplicationUser user, string stamp)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.SetSecurityStampAsync(identityUser, stamp);
        SetApplicationUser(user, identityUser);
        return task;
    }
    private static void SetApplicationUser(ApplicationUser user, IdentityUser identityUser)
    {
        user.PasswordHash = identityUser.PasswordHash;
        user.SecurityStamp = identityUser.SecurityStamp;
        user.Id = identityUser.Id;
        user.UserName = identityUser.UserName;
    }
    private IdentityUser ToIdentityUser(ApplicationUser user)
    {
        return new IdentityUser
        {
            Id = user.Id,
            PasswordHash = user.PasswordHash,
            SecurityStamp = user.SecurityStamp,
            UserName = user.UserName
        };
    }
}
Run Code Online (Sandbox Code Playgroud)
对于密码哈希和安全标记,我使用UserStore的实现使事情变得更简单.最后我们只需要更改AccountController的构造函数来利用我们的MyUserStore实现,
public AccountController()
    : this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
}
Run Code Online (Sandbox Code Playgroud)
删除用户表中不必要的列.你可以试试这样的事情
public partial class ModifyUser: DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.AspNetUsers", "NewField", c => c.String());
    }
    public override void Down()
    {
        DropColumn("dbo.AspNetUsers", "NewColumn");
    }
}
Run Code Online (Sandbox Code Playgroud)
然后在packageManager中运行 PM> update-database
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           3325 次  |  
        
|   最近记录:  |