如何向IdentityServer DI框架注册ApplicationUserManager?

2023-12-14

我正在使用 IdentityServer3 进行身份验证。所有用户都存储在 Sql DB 中,所以我也在使用Microsoft.AspNet.Identity实际身份验证的框架,出于同样的目的,我创建了自己的框架ApplicationUserManager class.

AspNet 身份将 IoC 功能集成到 OWIN 中间件中。并且它注册了ApplicationUserManager like:

 app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);  

它接受函数委托,返回一个新的实例ApplicationUserManager

public static ApplicationUserManager 
Create(IdentityFactoryOptions<ApplicationUserManager> options,
        IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

然而,IdentityServer 使用自己的 DI 框架并且(i think)我们不能使用静态Create()注册方法ApplicationUserManager还可以使用 IdentityServerCreate()方法需要IdentityFactoryOptions and a IOwinContext作为参数。

我跟着这个SO post我改变了实施ApplicationUserManager使用构造函数注入

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{   
    public ApplicationUserManager(ApplicationUserStore store, IdentityFactoryOptions<ApplicationUserManager> options)
        : base(store)
    {          
        // Configure validation logic for usernames
        UserValidator = new UserValidator<ApplicationUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        UserLockoutEnabledByDefault = true;
        DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        MaxFailedAccessAttemptsBeforeLockout = 5;

        EmailService = new EmailService();
        SmsService = new SmsService();

        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }        
}

然后将所有服务注册到IdentityServer自己的DI框架中,如下所示

factory.UserService = new Registration<IUserService, UserService>();
factory.Register(new Registration<ApplicationUserManager>());
factory.Register(new Registration<ApplicationUserStore>());
factory.Register(new Registration<IdentityFactoryOptions<ApplicationUserManager>>(resolver => new IdentityFactoryOptions<ApplicationUserManager>
            {
                DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET Identity")
            }));
factory.Register(new Registration<ApplicationDbContext>(resolver => new ApplicationDbContext(ApplicationConfig.ConnectionString)));

问题

  1. 这是正确的注册方式吗ApplicationUserManager和 IdentityServer的DI框架?
  2. 我正在创造DataProtectionProvider在注册期间和UserTokenProvider在构造函数内部。 IdentityServer 如何使用这两个提供程序?
  3. 请注意,我没有注册IOwinContext从那时起任何地方 ApplicationUserManager的构造函数不再需要它,将 这会导致 OWIN 管道出现任何问题吗?
  4. 什么是理想的注册模式ApplicationUserManager?

这两篇文章帮助解决了我的问题

http://tech.trailmax.info/2014/06/asp-net-identity-and-cryptographicexception-when-running-your-site-on-microsoft-azure-web-sites/

http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/

然而我的ApplicationUserManager位于单独的类库中并且startup.cs是在网络项目中。类库没有对 Web 项目的引用。所以我重构了ApplicationUserManager构造函数

    public ApplicationUserManager(ApplicationUserStore store, IDataProtectionProvider dataProtectionProvider)
        : base(store)
    {
       // other stuff


        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("UserToken"));
        }
    }

并且还注册了IDataProtectionProvider与DI框架。我没有使用 Unity 作为 IoC。我正在使用 IdentityServer 自己的 DI 框架。所以我注册IDataProtectionProvider as

  factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何向IdentityServer DI框架注册ApplicationUserManager? 的相关文章

  • 当我在 ASP.NET 中组合 Bearer Token 和 Cookie 身份验证时,出现 401

    我需要结合令牌和 cookie 来授权 wepapi 项目中的请求 我添加了 Cookie 和 Jwt 来验证请求 在更改 DefaultPolicy 之前 我可以获得我的声明 信息 但更改后我得到 401 这是我的 Program cs
  • 类型 IUserStore`1 没有可访问的构造函数

    我想使用 Unity 3 设置 MVC5 应用程序 我从标准模板创建了一个默认的 Web mvc5 应用程序 然后添加了 unity 当我访问 AccountController 中的注册操作时 出现以下异常 类型 IUserStore 1
  • C# 修改身份或添加声明

    应用程序是 Blazor Server NET Core 5 0 我正在使用 NET Core 的身份系统 但遇到了问题 我希望将这个人的名字与身份一起存储 以便我可以轻松地调用它 目前我有一个类覆盖基本身份 public class Ap
  • ASP.Net MVC:如何读取我的自定义声明值

    请参阅下面的代码 我知道通过这种方式我们可以将自定义数据添加到索赔中 但现在的问题是如何读回这些值 假设我想读回索赔价值电子邮件和电子邮件2请告诉我需要编写什么代码来读回索赔值电子邮件和电子邮件2 UserManager
  • ASP.NET身份重置密码

    如何在新的 ASP NET Identity 系统中获取用户的密码 或者在不知道当前密码的情况下如何重置 用户忘记密码 或者在不知道当前密码的情况下如何重置 用户忘记密码 如果您想使用 UserManager 更改密码 但不想提供用户的当前
  • OWIN 不记名令牌身份验证

    我有一些与不记名令牌相关的问题 在 欧文 您可以保护门票Protect ticket 像这样 ClaimsIdentity identity new ClaimsIdentity Startup OAuthServerOptions Aut
  • 删除声明同步身份 2.2

    我对 Identity 2 0 有问题 我正在尝试更新用户的声明 这是我的代码 var UserID User Identity GetUserId var claims await UserManager GetClaimsAsync U
  • 使用IdentityServer3调用Web API时出现401未经授权

    我正在尝试使用 IdentityServer3 和客户端凭据流程来设置一个简单的示例 该示例包含一个控制台客户端 使用从 IdentityServer 接收的令牌调用 Web API 资源 Web API 和 IdentityServer
  • ASP.Net Identity 2.0:用户是System.Web.Security.RolePrincipal,为什么?

    我正在尝试在现有应用程序中实现 Asp Net Identity 2 0 OWIN 但在角色方面我遇到了各种麻烦 我从项目模板创建了一个示例项目 并且 据我所知 我已将其中的所有内容复制到我的应用程序中 我修改了连接信息 以便身份验证表来自
  • 身份框架用户锁定

    我尝试在 3 次登录尝试失败后锁定用户登录 5 分钟 我已将这 3 行添加到App Start IdentityConfig cs public static ApplicationUserManager Create method man
  • 使用 Web API 服务作为中央身份验证点

    我对身份管理领域非常陌生 所以请饶恕我 我想要做的是让多个客户端 MVC 应用程序与单个 Web API 应用程序通信 以对其用户进行身份验证 在该 Web API 应用程序中 我想使用 ASP NET Identity 与用户与数据库进行
  • OnValidateIdentity 会话为空 - Mvc Owin

    目前 我在 OnValidateIdentity 中访问 Session 时遇到问题 HttpContext Current Session一片空白 怎么了 我的申请如下 我有 2 个项目 Mvc 与 WebApi 我希望用户在更改密码时注
  • User.Identity.GetUserId 是缓存还是每次都从数据库获取?

    我正在使用 ASP Net MVC 5 并且我在我的代码中调用这一行 string userId User Identity GetUserId ASP NET MVC 是否会在每次调用时从表中获取此数据 还是将其缓存 查看 Microso
  • Asp.net core - 没有这样的表:AspNetUsers

    所以我试图为我的 asp net core 应用程序实现用户登录 我正在关注微软教程here https docs asp net en latest security authentication identity html 我有两个上下
  • AspNet.Identity 自定义用户和自定义角色应该很简单;我缺少什么?

    使用来自的示例http www asp net identity http www asp net identity我已经走到这一步了 这RoleManager工作完美 我对待UserManager相同 我认为一切都是正确的 但我似乎无法更
  • 将列添加到 ASP.NET Identity 中的 AspNetUserClaims

    我在用着Microsoft AspNet Identity Core 2 2 1在我的解决方案中 我需要将其与另一个应自动添加声明的系统集成 为了跟踪哪些声明是手动添加的以及哪些是由外部系统创建的 我希望在我的AspNetUserClaim
  • AspNetUserLogins 表身份

    AspNetUserLogins 的用途是什么 是存储用户的登录信息吗 然后我如何用该数据更新该表 AspNetUserLogins 的用途是什么 在Asp net Identity中 Identity系统使用AspNetUserLogin
  • 如何使用 Entity Framework 和 Identity 解决对象处置异常 ASP.NET Core

    我正在尝试编写一个控制器 该控制器接收来自 AJAX 调用的请求并通过 DBContext 对数据库执行一些调用 但是 当我发出命令时var user await GetCurrentUserAsynch 在对 DBContext 的任何调
  • 将 VS2015 中的 ASP.NET Identity 中的 User Id 类型更改为 int

    默认情况下 VS 2015 中的 ASP NET Identity 使用字符串作为 AspNet 表的主键 我想使用 int 类型的 id 来代替 经过一些研究后发现 框架开箱即用地支持不同类型的 ID 在下面的答案中 我将展示要实现这一目
  • 如何强制 Asp.Net Identity 2.0 使用自定义角色而不是 IdentityUserRole

    我已经扩展了 IdentityUserRole 如下 public class ApplicationUserRoles IdentityUserRole

随机推荐