带有Aspnet标识和资源所有者的嵌入式IdentityServer 4

时间:2017-12-04 16:41:56

标签: asp.net-core asp.net-identity identityserver4

我正在尝试将IdentityServer4与资源所有者流+ aspnet身份一起使用,并将api嵌入到同一个项目中。

我测试了样本here on github,它运行正常。我能够为数据库中的注册用户检索令牌,并使用此令牌从api获取受保护的资源。

api与身份服务器分离的示例,一旦两者合并为一个项目,我仍然能够获得一个令牌,但我在尝试访问受保护资源时获得401 Unauthorized。不知何故,嵌入式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几乎总是嵌入身份服务器,是否有任何我可以用来使其工作的例子?

谢谢。

1 个答案:

答案 0 :(得分:0)

在深入研究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中的工作身份服务器。