作者:天呀你呀_778 | 来源:互联网 | 2022-12-06 19:27
我将由实体框架创建的AspNetRoles扩展为如下所示:
public class AspNetRoles:IdentityRole
{
public AspNetRoles() : base() { }
public String Label { get; set; }
public String ApplicationId { get; set; }
public AspNetApplications Application { get; set; }
public static readonly String SystemAdministrator = "SystemAdministrator";
}
我知道,因为我已经扩展了identityrole表,所以必须对usermanager进行更改。这是我所做的:
public class ApplicationUserManager : UserManager
{
public ApplicationUserManager(IUserStore store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore(context.Get()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator(manager)
{
AllowOnlyAlphanumericUserNames= false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNOnLetterOrDigit= true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectiOnProvider= options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager(), context.Authentication);
}
}
public class ApplicationRoleManager : RoleManager, IDisposable
{
public ApplicationRoleManager(RoleStore store) : base(store)
{ }
public static ApplicationRoleManager Create(IdentityFactoryOptions options, IOwinContext context)
{
//AppIdentityDbContext db = context.Get();
//AppRoleManager manager = new AppRoleManager(new RoleStore(db));
return new ApplicationRoleManager(new RoleStore(context.Get()));
//return manager;
}
}
public class ApplicationUserStore : UserStore, IUserStore, IUserStore, IDisposable where TUser : IdentityUser
{
public ApplicationUserStore(DbContext context) : base(context) { }
}
这是我的DBContext:
public class ApplicationDbContext : IdentityDbContext
{
public virtual DbSet AspNetUsersExtendedDetails { get; set; }
public virtual DbSet AspNetApplications { get; set; }
public virtual DbSet AspNetEventLogs { get; set; }
public ApplicationDbContext() : base("AppStudio")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
但是,在启动应用程序时出现此错误:
实体类型IdentityRole不是当前上下文模型的一部分。
我不确定为什么会这样。扩展角色表后,我是否错过了需要更改的内容?
1> Reza Aghaei..:
简短答案
上面代码中的主要问题在于的Create
方法UserManager
。在该方法中,您应该创建一个UserManager
使用,UserStore
它可以识别您创建的新角色类。为此,您可以使用拥有的ApplicationUserStore
类的实例,或通过以下方式创建新的用户存储:
new UserStore())
如何向IdentityRole添加自定义属性?
要将新属性添加到IdentityRole
,您可以按照以下步骤操作:
创建一个ASP.NET Web应用程序
确保选择“ MVC”,并且“ 身份验证”是“ 个人用户帐户”
转到模型文件夹?打开IdentityModels.cs并创建包含要添加的自定义属性的ApplicationRole类:
public class ApplicationRole : IdentityRole //My custom role class
{
public string ApplicationId { get; set; } //My custom property
}
更改类型为的参数的接受GenerateUserIdentityAsync
方法:ApplicationUser
UserManager
public class ApplicationUser : IdentityUser
{
public async Task GenerateUserIdentityAsync(UserManager manager)
{
更改ApplicationDbContext
基类并引入所有通用参数:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
生成项目。
转到“ 工具”菜单?Nuget软件包管理器?单击程序包管理器控制台
键入Enable-Migrations
并按Enter,直到任务完成。
键入Add-Migration "ApplicationRole"
并按Enter,直到任务完成。
键入Update-Database
并按Enter,直到任务完成。
转到App_Start文件夹?打开IdentityConfig.cs并更改ApplicationUserManager
要从UserManager
其派生的类 ,还更改其Create
方法以返回以下UserManage
信息ApplicationRole
:
public class ApplicationUserManager : UserManager
{
public ApplicationUserManager(IUserStore store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore(context.Get()));
要管理角色,请ApplicationRoleManager
在同一文件中创建类:
public class ApplicationRoleManager : RoleManager
{
public ApplicationRoleManager(IRoleStore store) : base(store) { }
public static ApplicationRoleManager Create(
IdentityFactoryOptions options,
IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore(context.Get()));
}
}
转到App_Start文件夹?打开Startup.Auth.cs并将以下代码添加到ConfigureAuth
方法中:
ConfigureAuthapp.CreatePerOwinContext(ApplicationRoleManager.Create);
现在该项目已准备好利用新项目ApplicationRole
。