コード例 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IHostingEnvironment env,
                              ILoggerFactory loggerFactory,
                              IApiVersionDescriptionProvider provider)
        {
            AppDefaultData.InitializeDatabase(app, Configuration);

            app.UseCors("default");

            #region Localization
            var locOptions = app.ApplicationServices.GetService <IOptions <RequestLocalizationOptions> >();
            app.UseRequestLocalization(locOptions.Value);
            #endregion

            if (env.IsDevelopment())
            {
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseBrowserLink();
                app.UseSwaggerUI(c =>
                {
                    foreach (var description in provider.ApiVersionDescriptions)
                    {
                        c.SwaggerEndpoint(
                            $"/swagger/{description.GroupName}/swagger.json",
                            description.GroupName.ToUpperInvariant());

                        c.OAuthClientId(AppDefaultData.TestClient.ClientId);
                        c.OAuthClientSecret(AppDefaultData.TestClient.ClientSecret);
                        c.OAuthAppName(AppDefaultData.TestClient.ClientName);
                        c.OAuth2RedirectUrl(AppDefaultData.TestClient.RedirectUris[0]);
                    }

                    c.DocExpansion(DocExpansion.None);
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = ctx =>
                {
                    const int durationInSeconds = 60 * 60 * 24;
                    ctx.Context.Response.Headers[HeaderNames.CacheControl] =
                        "public,max-age=" + durationInSeconds;
                }
            });

            // do not change the order here
            app.UseIdentityServer4MicroService();

            app.UseAuthentication();

            app.UseIdentityServer();

            app.UseMvcWithDefaultRoute();

            #region swagger
            app.UseSwagger(x =>
            {
                x.PreSerializeFilters.Add((doc, req) =>
                {
                    doc.Schemes  = new[] { "https" };
                    doc.Host     = Configuration["IdentityServer"];
                    doc.Security = new List <IDictionary <string, IEnumerable <string> > >()
                    {
                        new Dictionary <string, IEnumerable <string> >()
                        {
                            { "SubscriptionKey", new string[] { } },
                            { "AccessToken", new string[] { } },
                            { "OAuth2", new string[] { } },
                        }
                    };
                });
            });
            #endregion
        }
        public static IApplicationBuilder UseIdentityServer4MicroService(
            this IApplicationBuilder builder)
        {
            var env = builder.ApplicationServices.GetService <IHostingEnvironment>();

            var Configuration = builder.ApplicationServices.GetService <IConfiguration>();

            var options = builder.ApplicationServices.GetService <IdentityServer4MicroServiceOptions>();

            if (options.IdentityServer == null)
            {
                options.IdentityServer = new Uri(Configuration["IdentityServer"]);
            }

            builder.Validate();

            if (options.EnableCors)
            {
                builder.UseCors("cors-allowanonymous");
            }

            if (options.InitializeDatabase)
            {
                AppDefaultData.InitializeDatabase(builder, options.MicroServiceName, options.IdentityServer);
            }

            builder.UseMiddleware <TenantMiddleware>();

            builder.UseAuthentication();

            builder.UseIdentityServer();

            if (options.SwaggerGen)
            {
                builder.UseSwagger(x =>
                {
                    x.PreSerializeFilters.Add((doc, req) =>
                    {
                        doc.Schemes  = new[] { "https" };
                        doc.Host     = options.IdentityServer.Authority;
                        doc.Security = new List <IDictionary <string, IEnumerable <string> > >()
                        {
                            new Dictionary <string, IEnumerable <string> >()
                            {
                                { "SubscriptionKey", new string[] { } },
                                { "AccessToken", new string[] { } },
                                { "OAuth2", new string[] { } },
                            }
                        };
                    });
                });
            }

            if (options.SwaggerUI)
            {
                builder.UseSwaggerUI(c =>
                {
                    var provider = builder.ApplicationServices.GetService <IApiVersionDescriptionProvider>();

                    foreach (var description in provider.ApiVersionDescriptions)
                    {
                        c.SwaggerEndpoint(
                            $"/swagger/{description.GroupName}/swagger.json",
                            description.GroupName.ToUpperInvariant());

                        c.OAuthAppName(options.SwaggerUIClientName);
                        c.OAuthClientId(options.SwaggerUIClientID);
                        c.OAuthClientSecret(options.SwaggerUIClientSecret);
                        c.OAuth2RedirectUrl($"{options.IdentityServer.OriginalString}/swagger/oauth2-redirect.html");
                    }

                    c.DocExpansion(DocExpansion.None);
                });
            }

            if (options.EnableResponseCaching)
            {
                builder.UseResponseCaching();
            }

            return(builder);
        }