如何最好地覆盖MVC 5中的身份验证



我有一个项目没有注册。Administrator在admin中注册用户。项目没有角色,我只有一种类型的用户。我不需要"AspNetRoles"、"AspNetUserClaims"、"AstpNewUserLogins"、"阿斯pNetUserRoles"。在表"AspNetUsers"中,我只需要"Id"、"Email"、"Password"和一些自定义属性。在mvc 5中实现这一点的最佳方式是什么?

要向AspNetUsers添加更多列/字段,您需要在身份模型中添加这些列/字段并使用-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");
    }

当您使用ASPNETSchema进行用户注册时,我认为您无法避免声明、角色和其他表,但您可以忽略它们。

更新

为了避免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; }
}

接下来我们需要一个DbContet来存储用户,

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public virtual IDbSet<ApplicationUser> Users { get; set; }
}

然后我们需要实现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
        };
    }
}

对于密码散列和安全戳,我使用UserStore的实现来使事情变得更简单。最后,我们只需要更改AccountController的构造函数来利用我们的MyUserStore实现

public AccountController()
    : this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
}

在用户表中删除不必要的列。你可以试试这个这样的设计

public partial class ModifyUser: DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.AspNetUsers", "NewField", c => c.String());
    }
    public override void Down()
    {
        DropColumn("dbo.AspNetUsers", "NewColumn");
    }
}

然后在packageManager中运行PM> update-database

相关内容

  • 没有找到相关文章