热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在DI中注册自定义UserManager等-ASP.NETCore2.1

如何解决《如何在DI中注册自定义UserManager等-ASP.NETCore2.1》经验,为你挑选了1个好方法。

我需要创建自己的类来处理EF Core 2.1中的身份。我有以下内容,但是当要使用控制器的方法时,出现以下错误

尝试激活“ Sample.API.Models .Identity.Managers.ApplicationUserManager”时,无法解决“ Sample.API.Models.Identity.Stores.ApplicationUserStore”类型的服务。

public void ConfigureServices(IServiceCollection servicesCollection)
{
    servicesCollection.AddDbContext(currentOptiOns=>
        currentOptions.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    servicesCollection.AddIdentity()
        .AddEntityFrameworkStores()
        .AddRoleStore()
        .AddUserStore()
        .AddUserManager()
        .AddRoleManager()
        .AddSignInManager()
        .AddDefaultTokenProviders();

        servicesCollection.AddTransient, ApplicationUserManager>();
        servicesCollection.AddTransient, ApplicationSignInManager>();
        servicesCollection.AddTransient, ApplicationRoleManager>();
        servicesCollection.AddTransient, ApplicationUserStore>();
        servicesCollection.AddTransient, ApplicationRoleStore>();

        ...

        ...

        ...
}

public class MyIndentityContext : IdentityDbContext
{
    private readonly IConfiguration _configuration;

    private readonly IHttpContextAccessor _httpContextAccessor;

    public MyIndentityContext(DbContextOptions dbContextOptions, IHttpContextAccessor httpContextAccessor,
        IConfiguration configuration)
        : base(dbContextOptions)
    {
        _cOnfiguration= configuration;
        _httpCOntextAccessor= httpContextAccessor;
    }

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

        modelBuilder.HasDefaultSchema("Sample.API");
    }

}

public class ApplicationRoleManager : RoleManager
{
    public ApplicationRoleManager(ApplicationRoleStore roleStore,
        IEnumerable> roleValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors, ILogger logger) : base(roleStore,
        roleValidators,
        keyNormalizer, errors, logger)
    {
    }
}

public class ApplicationSignInManager : SignInManager
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IHttpContextAccessor contextAccessor,
        IUserClaimsPrincipalFactory claimsFactory, IOptions optionsAccessor,
        ILogger logger, IAuthenticationSchemeProvider schemes) : base(userManager,
        contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
    {
    }
}

public class ApplicationUserManager : UserManager
{
    public ApplicationUserManager(ApplicationUserStore userStore, IOptions optionsAccessor,
        IPasswordHasher passwordHasher,
        IEnumerable> userValidators,
        IEnumerable> passwordValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors, IServiceProvider services, ILogger logger) :
        base(userStore, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,
            services, logger)
    {
    }
}

public class ApplicationRoleStore : RoleStore
{
    public ApplicationRoleStore(MyIndentityContext dbContext, IdentityErrorDescriber identityErrorDescriber)
        : base(dbContext, identityErrorDescriber)
    {}
}

public class ApplicationUserStore : UserStore
{
    public ApplicationUserStore(MyIndentityContext dbContext, IdentityErrorDescriber identityErrorDescriber)
        : base(dbContext, identityErrorDescriber)
    {}

}

public class ApplicationUser : IdentityUser {}

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() { }

    public ApplicationRole(string roleName) : base(roleName) { }

    public ApplicationRole(string roleName, string roleDescription) : base(roleName)
    {
        Description = roleDescription;
    }

}

[Authorize]
[ApiController]
[Route("api/[controller]")]
[EnableCors(CORS.AllowSpecificOrigins)]

public class UserController : BaseController
{
    private readonly ApplicationUserManager _applicationUserManager;

    public UserController(ApplicationUserManager applicationUserManager)
    {
        _applicatiOnUserManager= applicationUserManager;
    }

     // GET: api/User/5
    [HttpGet("{id}")]
    public async Task Get(int id)
    {

        var currentuser = await _applicationUserManager.FindByNameAsync("NAME");

        return ...;

    }

}

编辑:

完成“ Camilo Terevinto”用户的建议后,我使它起作用,但我还必须更改所有管理器的构造函数。例如,我必须通过IRoleStore roleStore更改ApplicationRoleStore roleStore



1> SO used to b..:

使用依赖注入时,具有依赖关系的类不要求实现,而是要求合同。

这个:

private readonly ApplicationUserManager _applicationUserManager;

public UserController(ApplicationUserManager applicationUserManager)
{
    _applicatiOnUserManager= applicationUserManager;
}

应该:

private readonly UserManager _applicationUserManager;

public UserController(UserManager applicationUserManager)
{
    _applicatiOnUserManager= applicationUserManager;
}

另外,请注意,您不需要任何这些行,这些行已由先前对其.AddIdentity和选项的调用添加。

servicesCollection.AddTransient, ApplicationUserManager>();
servicesCollection.AddTransient, ApplicationSignInManager>();
servicesCollection.AddTransient, ApplicationRoleManager>();
servicesCollection.AddTransient, ApplicationUserStore>();
servicesCollection.AddTransient, ApplicationRoleStore>();


推荐阅读
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • React项目中运用React技巧解决实际问题的总结
    本文总结了在React项目中如何运用React技巧解决一些实际问题,包括取消请求和页面卸载的关联,利用useEffect和AbortController等技术实现请求的取消。文章中的代码是简化后的例子,但思想是相通的。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
author-avatar
liuxiaoli8611
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有