asp.net – AddToRole和IdentityRole不是当前上下文模型的一部分
发布时间:2020-12-16 09:12:19 所属栏目:asp.Net 来源:网络整理
导读:我正在使用Identity 2.1来处理我的asp.net应用程序中的用户角色.到目前为止,我创建了从IdentityDBContext扩展的新上下文,扩展了IdentityUser和IdentityRole以添加几个新字段.但是,每当我尝试使用UserManager将特定角色添加到特定角色时,实体类型IdentityRole
我正在使用Identity 2.1来处理我的asp.net应用程序中的用户角色.到目前为止,我创建了从IdentityDBContext扩展的新上下文,扩展了IdentityUser和IdentityRole以添加几个新字段.但是,每当我尝试使用UserManager将特定角色添加到特定角色时,实体类型IdentityRole不是当前上下文的模型的一部分.所以用户角色关系似乎有问题,这里是我的代码到目前为止参考:
用户 public class User : IdentityUser{ public string Name { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager,string authenticationType) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this,authenticationType); // Add custom user claims here return userIdentity; } } 角色 public partial class Role : IdentityRole { public string Description { get; set; } } 数据库上下文 namespace Carbon.Models { public partial class CarbonEDM : IdentityDbContext<User,Role,string,IdentityUserLogin,IdentityUserRole,IdentityUserClaim>{ public CarbonEDM() : base("name=CarbonDB") { } public static CarbonEDM Create() { return new CarbonEDM(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<User>().ToTable("Users","dbo"); modelBuilder.Entity<Role>().ToTable("Roles","dbo"); modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles","dbo"); modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims","dbo"); modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins","dbo"); } } } ApplicationUserManager public class ApplicationUserManager : UserManager<User> { public ApplicationUserManager(IUserStore<User> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,IOwinContext context) { var manager = new ApplicationUserManager(new UserStore<User>(context.Get<CarbonEDM>())); // Configure validation logic for usernames manager.UserValidator = new UserValidator<User>(manager) { AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 6,RequireNonLetterOrDigit = false,RequireDigit = false,RequireLowercase = false,RequireUppercase = false,}; // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } } 最后,只要在迁移过程中出现错误: protected override void Seed(Carbon.Models.CarbonEDM context) { DbContextTransaction transaction = null; transaction = context.Database.BeginTransaction(); if (!context.Roles.Any()) { var roleStore = new RoleStore<Role>(context); var roleManager = new RoleManager<Role>(roleStore); roleManager.Create(new Role { Name = "Admin" }); roleManager.Create(new Role { Name = "Control Unit Operator" }); roleManager.Create(new Role { Name = "Lab Operator" }); roleManager.Create(new Role { Name = "Production Engineer" }); roleManager.Create(new Role { Name = "Production Operator" }); } if (!context.Users.Any()) { var userStore = new UserStore<User>(context); var userManager = new ApplicationUserManager(userStore); var _user = new User { Email = "yehiasalam@live.com",PhoneNumber = "+20 12 23461340",Name = "Yehia A.Salam",UserName = "yehiasalam@live.com" }; userManager.Create(_user,"pass@word"); userManager.AddToRole(_user.Id,"Admin"); /* IdentityRole error here */ } context.SaveChanges(); transaction.Commit(); base.Seed(context); } } 对不起,很长的帖子,我试图包括一切. 解决方法
好吧所以问题出在UserStore上,默认声明是
public class UserStore<TUser> : UserStore<TUser,IdentityRole,IdentityUserClaim>,IUserStore<TUser>,IUserStore<TUser,string>,IDisposable where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser { 它具有显式声明的IdentityRole,而不是使用像TUser这样的泛型表示法,因此我们只需要创建从父UserStore扩展的另一个类: public class CarbonUserStore<TUser> : UserStore<TUser,IDisposable where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser { public CarbonUserStore(DbContext context) :base(context) {} } 微软并不酷,我花了一天的时间试图让它等待.但是,它已添加到随vNext发送的身份包中(必须喜欢它开源): public class UserStore<TUser,TRole,TContext> : UserStore<TUser,TContext,string> where TUser : IdentityUser,new() where TRole : IdentityRole,new() where TContext : DbContext { public UserStore(TContext context,IdentityErrorDescriber describer = null) : base(context,describer) { } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 上载ASP.NET MVC Web api控制器中的文件
- asp.net – 将WPF转换为Web
- 在asp.net中按日期对directoryinfo中的文件进行排序
- PagesSection.ValidateRequest是否足以阻止asp.Net中的XSS
- 如何检查ASP.Net中客户端(JavaScript)的Page.Validate()?
- asp.net – 单元/集成测试,它们应该是多么精细?
- asp.net-mvc – 如何编辑MVC4表单中的子对象?
- ASP.NET MVC的GPL 2.0免费Telerik扩展
- asp.net – iFrame中的Response.Redirect(),重定向父窗口
- asp.net – 无效的回发或回调参数.单击按钮时
推荐文章
站长推荐
- ASP.NET MVC中的jQuery AJAX响应
- 配置 – ASP.NET Web Api:项目需要SQL Server E
- ASP.NET站点移动到IIS7会导致页面输出中出现乱码
- 如何使用asp.net web.sitemap和菜单控件与bootst
- .net – 绕过获取请求的MVC验证
- Autofac和ASP.NET Web API ApiController
- asp.net – 如果Ninject尚未绑定,如何在Ninject中
- asp.net-mvc – 我的MVC 5模板中没有Application
- 使用System.Net.Mail中的SMTP发送邮件(带附件)
- asp.net-mvc – MVC .net属性上必需属性的布尔值
热点阅读