C# 如何注册自定义UserStore&;DI中的用户管理器
以下是我的设置:C# 如何注册自定义UserStore&;DI中的用户管理器,c#,asp.net-core,asp.net-core-mvc,asp.net-identity-3,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Identity 3,以下是我的设置: public class ApplicationUser : IdentityUser<Guid> { } public class ApplicationRole : IdentityRole<Guid> { } public class ApplicationUserLogin : IdentityUserLogin<Guid> { } public class ApplicationUserClaim : IdentityUserCla
public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationUserLogin : IdentityUserLogin<Guid>
{
}
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}
public class ApplicationRoleClaim : IdentityRoleClaim<Guid>
{
}
公共类应用程序用户:IdentityUser
{
}
公共类应用程序角色:IdentityRole
{
}
公共类ApplicationUserLogin:IdentityUserLogin
{
}
公共类ApplicationUserClaim:IdentityUserClaim
{
}
公共类应用程序角色声明:IdentityRoleClaim
{
}
下面是我的UserStore的定义
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, MyContext, Guid>
{
public ApplicationUserStore(MyContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{
}
}
公共类ApplicationUserStore:UserStore
{
公共应用程序存储库(MyContext上下文,IdentityErrorDescriber describer=null)
:base(上下文、描述符)
{
}
}
下面是我的UserManager的定义
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, IEnumerable<IUserTokenProvider<ApplicationUser>> tokenProviders,
ILoggerFactory logger, IHttpContextAccessor contextAccessor)
: base(
store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,
tokenProviders, logger, contextAccessor)
{
}
}
公共类应用程序管理员:用户管理员
{
公共应用程序服务器管理器(IUserStore存储、IOOptions访问器、,
IPasswordHasher密码hasher,IEnumerable用户验证程序,
IEnumerable PasswordValidator、ILookupNormalizer、keyNormalizer、,
IdentityErrorDescriber错误,IEnumerable tokenProviders,
iLogger工厂记录器,IHttpContextAccessor上下文访问器)
:基本(
store、OptionAccessor、passwordHasher、userValidators、passwordValidators、keyNormalizer、errors、,
令牌提供程序、记录器、上下文访问器)
{
}
}
以下是my DbContext的定义:
public class MyContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
公共类MyContext:IdentityDbContext
{
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
}
}
这是我的Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.Get("Data:DbConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyContext, Guid>()
.AddUserStore<ApplicationUserStore>()
.AddRoleStore<ApplicationRoleStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>()
.AddDefaultTokenProviders();
var builder = new ContainerBuilder();
builder.Populate(services);
var container = builder.Build();
return container.Resolve<IServiceProvider>();
}
公共IServiceProvider配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(options=>options.UseSqlServer(Configuration.Get(“Data:DbConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddUserStore()
.AddRoleStore()
.AddUserManager()
.AddRoleManager()
.AddDefaultTokenProviders();
var builder=new ContainerBuilder();
建造商。填充(服务);
var container=builder.Build();
返回container.Resolve();
}
此构造函数的依赖项将起作用:
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
public AccountController(UserManager UserManager,SignInManager SignInManager)
这个不会:
public AccountController(ApplicationUserManager userManager, SignInManager<ApplicationUser> signInManager)
public AccountController(ApplicationUserManager用户管理器、SignInManager SignInManager)
有人知道我做错了什么吗?DI通常用于接口驱动的开发
.AddUserManager()
指定实现UserManager
,而不是服务接口。这意味着它仍然希望您获得UserManager
,并且只以这种方式使用它;它将为您提供一个ApplicationUserManager
我假设您想在应用程序SerManager
上使用其他方法如果没有,只需按照依赖项构造函数的工作方式使用它,并享受接口驱动的开发。如果是,您有3个选项:
UserManager
继承,而是将ApplicationUserManager
作为包装类编写;您可以将其包含在构造函数中。这将为您提供ApplicationUserManager
内部所需的所有功能UserManager
本身没有真实状态:
services.AddScoped<ApplicationUserManager>();
services.addScope();
这里的缺点是,对于用户范围,实际上有两个UserManager
对象;结果可能会导致一些低效率。从当前代码的状态来看,我认为不是UserManager
的基本功能,那么这可能非常复杂我现在使用ASP.NET Core 1.1,此行为已被修复 我可以轻松实现自己的UserManager和UserStore,然后按如下方式引导应用程序:
// identity models
services
.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext, Guid>()
.AddUserManager<ApplicationUserManager>()
.AddUserStore<ApplicationUserStore>()
.AddDefaultTokenProviders();
//身份模型
服务
.AddIdentity()
.AddEntityFrameworkStores()
.AddUserManager()
.AddUserStore()
.AddDefaultTokenProviders();
并将UserManager和UserStore都注入到我的控制器中,没有任何问题:
public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IHttpContextAccessor httpContextAccessor,
ApplicationUserManager userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory)
{
_interaction = interaction;
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<AccountController>();
_account = new AccountService(_interaction, httpContextAccessor, clientStore);
}
公共帐户控制器(
IIIdentityServerInteractionService交互,
IClientStore客户端商店,
IHttpContextAccessor httpContextAccessor,
ApplicationUserManager用户管理器,
SignInManager SignInManager,
电邮发送者,
伊斯姆森德·斯姆森德,
伊洛格工厂(伐木厂)
{
_互动=互动;
_userManager=userManager;
_signInManager=signInManager;
_emailSender=emailSender;
_smsSender=smsSender;
_logger=loggerFactory.CreateLogger();
_account=新AccountService(_交互,httpContextAccessor,clientStore);
}
我想到了这个:
// Extract IApplicationUserManager interface with all methods you are using
public class ApplicationUserManager : UserManager<ApplicationUser>, IApplicationUserManager
// Register your custom user manager
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddUserManager<ApplicationUserManager>();
// Return same scoped instance whenever you injecting your custom user manager into any constructor
services.AddScoped<IApplicationUserManager>(s => s.GetService<ApplicationUserManager>());
//使用您正在使用的所有方法提取IAApplicationUserManager接口
公共类ApplicationUserManager:UserManager、IAApplicationUserManager
//注册您的自定义用户管理器
服务.额外性()
.AddUserManager();
//在将自定义用户管理器注入任何构造函数时,返回相同的作用域实例
services.addScope(s=>s.GetService());
完全正确,我实际上要修复这个“soonish”,您在Microsoft.AspNet.Identity.Core中引用了哪个nuget软件包版本?我试图创建一个CustomUsermanager,但是当我从UserManager继承时,构造函数要求9个参数,但是你的构造函数需要10个参数。。?我正在使用AspDotNetCoreFullFramework。IUserTokenProvider
无法识别!谢谢你的回答,让我来回答