热门标签 | 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>();


推荐阅读
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
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社区 版权所有