本文介绍了如何强制Asp.Net Identity 2.0使用自定义角色而不是IdentityUserRole的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了IdentityUserRole,如下所示:

I have extended the IdentityUserRole which is as follow

public class ApplicationUserRoles : IdentityUserRole<string>
{
    [Key]
    public string ApplicationId { get; set; }

    public virtual AspNetApplications AspNetApplications { get; set; }
}

和我的AspNetApplications类如下

and my AspNetApplications Class is as follow

public class AspNetApplications
{
    [Key]
    public string ApplicationId { get; set; }
    public string ApplicationName { get; set; }
}

迁移已在数据库中创建了AspNetApplications和ApplicationUserRoles表.屏幕截图如下.

Migration has created the AspNetApplications and ApplicationUserRoles tables in DB. A screen shot is as follow.

以下是我的身份模型

public class ApplicationUser : IdentityUser
{
    public virtual AspNetApplications AspNetApplication { get; set; }
    public virtual ApplicationUserRoles AspNetUserRoles { get; set; }
    //public virtual ICollection<AspNetApplicationUsers> AspNetApplicationUsers { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {

        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here

        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<AspNetApplications> AspNetApplications { get; set; }
    public DbSet<ApplicationUserRoles> AspNetUserRoles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUserRoles>().HasKey(m => new { m.ApplicationId, m.UserId, m.RoleId });
    }

}

到目前为止,一切都很好,但是现在当我在帐户控制器中检查用户时,它仍然从AspNetUserRoles带来基于角色的信息.请任何人告诉我如何使用自定义的ApplicationUserRoles代替IdentityUserRole.

everything is good so far but now when I inspect the User in my Account Controller, it is still bringing Role based information from AspNetUserRoles. Could please anyone tell me how I can make use of my custom ApplicationUserRoles instead of IdentityUserRole.

推荐答案

如果您查看IdentityUser的签名(您的ApplicationUser继承自它)

If you look at the signature of IdentityUser (your ApplicationUser inherits from it)

public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{

}

您会看到它接受了IdentityUserRole的自定义定义.

you can see that it accepts your custom definition of IdentityUserRole.

您必须实现的类MyApplicationUser必须实现正确的类型:

Your class MyApplicationUser you have to implement must implement the right type:

public class MyApplicationUser : IdentityUser<string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}

和您的角色:

public class MyRole : IdentityRole<string, ApplicationUserRoles>
{
}

和您的ApplicationDbContext:

public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}

和您的UserStore:

public class MyUserStore:  UserStore<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{
    public MyUserStore(MyContext context)
        : base(context)
    {
    }
}

应该是这样,我猜.有一个github repo ,我在其中玩了一些自定义类,自定义表格名称.

That should be it, I guess. There's a github repo where I've played a bit with custom class and custom tables names.

这篇关于如何强制Asp.Net Identity 2.0使用自定义角色而不是IdentityUserRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 10:32