0

我很难尝试构建一个所有这些东西都可以很好地结合在一起的单个项目。

  • 温泉
    • 静态文件
  • Web API (MVC)
    • 大摇大摆 (Swashbuckle)
  • 身份服务器4
    • IdentityServer4.AspNetIdentity
    • IdentityServer4.EntityFramework
    • 社交登录

我从 IdentityServer4.Samples\Quickstart6_AspNetIdentity 代码开始,在 nuget 之后集成了 nuget。

            // spa
            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseCors("any");

            // api
            app.Map(new PathString("/api"), map =>
            {
                map.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
                {
                    Authority = PublicHostUri,
                    ScopeName = ScopeName,
                    ScopeSecret = "secret",

                    RequireHttpsMetadata = PublicHostUri.ToLower().StartsWith("https"),
                    SaveToken = true,
                    EnableCaching = true
                });

                map.UseMvc();

            });
                // sts
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

                app.UseIdentity();
                app.UseIdentityServer();

                // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,

                    AutomaticAuthenticate = false,
                    AutomaticChallenge = false
                });

                app.UseTwitterAuthentication(new TwitterOptions
                {
                    ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g",
                    ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI"
                });

                app.UseGoogleAuthentication(new GoogleOptions
                {
                    AuthenticationScheme = "Google",
                    DisplayName = "Google",
                    SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,

                    ClientId = "434483408261-55tc8n0cs4ff1fe21ea8df2o443v2iuc.apps.googleusercontent.com",
                    ClientSecret = "3gcoTrEDPPJ0ukn_aYYT6PWo"
                });                          

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

            app.UseSwagger();
            app.UseSwaggerUi();

我正在努力隔离 Web API 部分,以使承载 AuthenticationScheme 是 /api 管道中唯一有效的身份验证。

现在所有添加的 AuthenticationSchemes,如 Cookie、Google、Twitter、OIDC、Bearer 都用于 /api 路由。

我想念什么?为什么 /api 不仅使用 UseIdentityServerAuthentication 又名不记名令牌方案?

更新:我在这里分享了概念代码的工作证明https://github.com/ChrisRichner/CoreWebApp.Quickstart

4

1 回答 1

2

您不能使用Map分支应用程序。它仅适用于指定路径。尝试使用MapWhen

        // api
        app.MapWhen(context => context.Request.Path.Value.StartsWith("/api"), builder=>
        {
            builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = PublicHostUri,
                ScopeName = ScopeName,
                ScopeSecret = "secret",

                RequireHttpsMetadata = PublicHostUri.ToLower().StartsWith("https"),
                SaveToken = true,
                EnableCaching = true
            });

            builder.UseMvc();

        });
于 2016-09-20T12:59:38.303 回答