gpt4 book ai didi

asp.net-mvc - 如何强制 Asp.Net Identity 2.0 使用自定义角色而不是 IdentityUserRole

转载 作者:行者123 更新时间:2023-12-01 09:21:09 25 4
gpt4 key购买 nike

我已经扩展了 IdentityUserRole 如下

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

public virtual AspNetApplications AspNetApplications { get; set; }
}

我的 AspNetApplications 类如下

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

Migration 在 DB 中创建了 AspNetApplications 和 ApplicationUserRoles 表。屏幕截图如下。

enter image description here

以下是我的身份模型

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 });
}

}

到目前为止一切都很好,但现在当我在我的帐户 Controller 中检查用户时,它仍然从 AspNetUserRoles 带来基于角色的信息。谁能告诉我如何使用我的自定义 ApplicationUserRoles 而不是 IdentityUserRole。

最佳答案

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

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 的自定义定义。

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

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我在其中玩过一些自定义类和自定义表名。

关于asp.net-mvc - 如何强制 Asp.Net Identity 2.0 使用自定义角色而不是 IdentityUserRole,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com