Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何最好地覆盖MVC5中的身份验证?_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

C# 如何最好地覆盖MVC5中的身份验证?

C# 如何最好地覆盖MVC5中的身份验证?,c#,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Identity,我有一个项目,其中没有注册。管理员在管理员中注册用户。项目没有角色,我只有一种类型的用户。我不需要“AspNetRoles”、“AspNetUserClaims”、“AspNewUserLogins”、“AspNetUserRoles”。在“AspNetUsers”表中,我只需要“Id”、“Email”、“Password”和一些自定义属性。在mvc 5中实现这一点的最佳方法是什么?要将更多列/字段添加到AspNetUsers中,您需要使用-update database命令在标识模型中添加这些

我有一个项目,其中没有注册。管理员在管理员中注册用户。项目没有角色,我只有一种类型的用户。我不需要“AspNetRoles”、“AspNetUserClaims”、“AspNewUserLogins”、“AspNetUserRoles”。在“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");
    }
接下来我们需要一个DbContet来存储用户

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

然后在packageManager中运行
PM>更新数据库

@kriper更新的解决方案以避免索赔和角色非常感谢您的详细描述,但是如何删除Identity 2.1中不必要的字段?ApplicationUser实现IdentityUser。@kriper您可以尝试类似我在回答中更新的迁移脚本之类的方法。我认为仅仅为了删除这些表而删除这些表是大量额外的工作,对项目没有任何价值。作为一名项目经理,我会说“让他们留在那里,让他们去吧,他们不要求钱。”。
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public virtual IDbSet<ApplicationUser> Users { get; set; }
}
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
        };
    }
}
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");
    }
}