具有Aspnet身份和资源所有者的嵌入式IdentityServer 4

dafriskymonkey

我试图将IdentityServer4与资源所有者流+ aspnet标识一起使用,并将api嵌入同一项目中。

在github上测试了示例并且工作正常。我能够为数据库中的注册用户检索令牌,并使用此令牌从api获取受保护的资源。

api的示例与身份服务器分离,一旦两者都合并到一个项目中,我仍然能够获得令牌,但是Unauthorized在尝试访问受保护的资源时却获得了401 嵌入式api不再以某种方式不再验证令牌。

这是Startup.cs代码:

// This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        //(1)
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc(config =>
        {
            var policy = new AuthorizationPolicyBuilder()
                         .RequireAuthenticatedUser()
                         .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });

        services
            .AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
        //(2)
        .AddAspNetIdentity<ApplicationUser>();
        //.AddTestUsers(Config.GetUsers());

        var corsBuilder = new CorsPolicyBuilder();
        corsBuilder.AllowAnyHeader();
        corsBuilder.AllowAnyMethod();
        corsBuilder.AllowAnyOrigin();
        corsBuilder.AllowCredentials();
        corsBuilder.WithExposedHeaders("Location");

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy", corsBuilder.Build());
        });

        services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();

        services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:51318";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api";
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseCors("CorsPolicy");

        app.UseIdentityServer();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

请注意,如果我们在内存中切换TestUser而不是ApplicationUser通过注释代码(1)并将代码更改为来持久存储(2)

    //(2)
    //.AddAspNetIdentity<ApplicationUser>();
    .AddTestUsers(Config.GetUsers());

整个系统正常工作,并且嵌入式api可以正常验证用户身份。

这段代码中缺少什么吗?在现实生活中,由于成本效益,该api几乎总是嵌入到身份服务器中,请问有什么示例可以使它起作用?

谢谢。

dafriskymonkey

深入研究AspNet Identity源代码后,我意识到该AddIdentity扩展正在做一些额外的工作来阻止验证令牌,但是如果没有令牌和AddEntityFrameworkStores身份管理器方法,则不能通过依赖项注入来设置身份管理器。

所以我们需要替换:

        services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

通过一段只执行依赖注入的代码:

        services.TryAddScoped<IUserValidator<ApplicationUser>, UserValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordValidator<ApplicationUser>, PasswordValidator<ApplicationUser>>();
        services.TryAddScoped<IPasswordHasher<ApplicationUser>, PasswordHasher<ApplicationUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IRoleValidator<IdentityRole>, RoleValidator<IdentityRole>>();
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>();
        services.TryAddScoped<UserManager<ApplicationUser>, AspNetUserManager<ApplicationUser>>();
        services.TryAddScoped<SignInManager<ApplicationUser>, SignInManager<ApplicationUser>>();
        services.TryAddScoped<RoleManager<IdentityRole>, AspNetRoleManager<IdentityRole>>();

        services.TryAddScoped<IRoleStore<IdentityRole>, RoleStore<IdentityRole>>();
        services.TryAddScoped<DbContext, ApplicationDbContext>();
        services.TryAddScoped<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();

通过这样做,最终结果是使用AspNet Identity将可工作的身份服务器嵌入api。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Identity Server 4-资源所有者密码授予和Google身份验证

Identityserver4 与资源所有者密码在资源服务器中返回 401

使用 IdentityServer4 和 Xamarin Android 使用资源所有者密码登录会话超时通知

将IdentityServer4与资源所有者密码授予一起使用

Ext4是否存储所有者和组名?还是只有ID?

具有嵌入式系统的STM32 Arm编程的STM32F103RB和STM32F4

具有Windows身份验证和自定义声明的IdentityServer4

具有Google身份验证和邮递员的IdentityServer4

带有aspnet核心MVC OIDC身份验证的IdentityServer4 - api范围限制?

检索所有嵌入式资源未下载资源

带有嵌入式 x509 证书的 xades4j 验证问题

具有多种消息类型的嵌入式 RTOS 生产者和消费者

具有多个实例的IdentityServer4

具有定制ConfigurationDBContext的IdentityServer4

nfs4权限被拒绝:显示所有者的UID GID号。我期待弦乐

我如何获取samba 4来保留所有者的执行位

Rails 4 public_activity与两种类型的所有者

不能是新格式化的ext4驱动器的所有者

Rust 中 &4 等字面引用的所有者是什么?

如何通过mount命令设置所有者ext4?

具有IdentityServer4,Asp.Net Core身份和不带实体框架的自定义提供程序的Blazor WebAssembly身份验证

具有Windows / AD身份验证的IdentityServer4无法正常工作?

具有ASP.NET Core身份的Web API的IdentityServer4基于角色的授权

“资源所有者密码流”和“隐式授予”之间的区别

Hibernate 4 NonUniqueObjectException嵌入式ID

如果使用用户和所有者密码加密,如何使用用户密码在java中解密128位RC4 pdf文件

带有Neo4j 3.0的Spring Data Neo4j 4:如何将配置传递给嵌入式驱动程序?

OAuth资源所有者密码流和HMAC

找不到索引提供者空间:嵌入式Neo4j