示例#1
0
        public static IApplicationBuilder AddOpenApiDocumentation(this IApplicationBuilder app, Action <ApplicationOptions> appOptions, Action <SingleSignOnOptions> ssoOptions, Action <OpenApiDocumentationOptions> openApiOptions)
        {
            var openApiSettings = new OpenApiDocumentationOptions();

            openApiOptions(openApiSettings);

            var appSettings = new ApplicationOptions();

            appOptions(appSettings);

            var ssoSettings = new SingleSignOnOptions();

            ssoOptions(ssoSettings);

            Log.Verbose("Configured OpenAPI on path {documentPath}", openApiSettings.Path);

            return(app

                   .UseOpenApi(settings => { settings.DocumentName = settings.DocumentName; })

                   .UseSwaggerUi3(settings =>
            {
                settings.Path = openApiSettings.Path;
                settings.OAuth2Client = new OAuth2ClientSettings
                {
                    AppName = appSettings.Name,
                    ClientId = ssoSettings.ClientId
                };
                settings.OAuth2Client.AdditionalQueryStringParameters.Add("nonce", "123456");
            }));
        }
示例#2
0
        public static IHostBuilder AddSingleSignOn(this IHostBuilder hostBuilder, Action <HostBuilderContext, SingleSignOnOptions> options, Action <HostBuilderContext, TokenValidationParameters> tokenValidationParametersBuilder)
        {
            var ssoOptions = new SingleSignOnOptions();
            var tokenValidationParameters = new TokenValidationParameters();

            return(hostBuilder.ConfigureServices((context, services) =>
            {
                options(context, ssoOptions);
                tokenValidationParametersBuilder(context, tokenValidationParameters);

                IdentityModelEventSource.ShowPII = ssoOptions.ShowPII;

                if (string.IsNullOrWhiteSpace(ssoOptions.Authority))
                {
                    throw new ArgumentNullException(nameof(ssoOptions.Authority));
                }

                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(opt =>
                {
                    opt.MetadataAddress = $"{ssoOptions.Authority}/.well-known/openid-configuration";
                    opt.Audience = ssoOptions.Audience;
                    opt.RequireHttpsMetadata = ssoOptions.RequireHttpsMetadata;
                    opt.SaveToken = ssoOptions.SaveToken;
                    opt.TokenValidationParameters = tokenValidationParameters;
                });
            }));
        }
示例#3
0
        public static IHostBuilder AddOpenApiDocumentation(this IHostBuilder hostBuilder, Action <HostBuilderContext, ApplicationOptions> appOptions, Action <HostBuilderContext, SingleSignOnOptions> ssoOptions)
        {
            return(hostBuilder.ConfigureServices((context, services) =>
            {
                var app = new ApplicationOptions();
                appOptions(context, app);
                Log.Verbose("Adding OpenAPI service for application {appName}", app.Name);

                var sso = new SingleSignOnOptions();
                ssoOptions(context, sso);
                Log.Verbose("Adding OpenAPI security using authority {authority}", sso.Authority);

                services.AddOpenApiDocument(options =>
                {
                    options.DocumentName = app.Name;
                    options.Title = app.Title;
                    options.Version = app.Version;

                    options.OperationProcessors.Add(new OperationSecurityScopeProcessor(sso.SecuritySchemeName));

                    options.DocumentProcessors.Add(new SecurityDefinitionAppender(sso.SecuritySchemeName, new NSwag.OpenApiSecurityScheme
                    {
                        Name = sso.SecuritySchemeName,
                        Scheme = sso.SecurityScheme,
                        Type = sso.SecuritySchemeType,
                        Flow = sso.OAuthFlow,
                        AuthorizationUrl = $"{sso.Authority}/protocol/openid-connect/auth",
                        Scopes = new Dictionary <string, string>
                        {
                            { "openid", "Open ID" },
                            { "profile", "Profile" }
                        }
                    }));
                });
            }));
        }
示例#4
0
        public static IHostBuilder PrebuildDefaultApp(this IHostBuilder hostBuilder, Action <HostBuilderContext, LoggerConfiguration> logger)
        {
            return(hostBuilder

                   .AddAutofac()
                   .AddSerilog(logger)

                   .AddHealthChecks((context, builder) => builder
                                    .AddCheck("Self", () => HealthCheckResult.Healthy(), new List <string> {
                "Liveness"
            })
                                    )
                   .AddHttpMetrics()

                   .AddOpenApiDocumentation(
                       (context, app) => context.Configuration.Bind("Application", app),
                       (context, sso) => context.Configuration.Bind("SingleSignOn", sso))

                   .AddRequests()
                   .AddRequestValidation()

                   .AddHttpRequestHandler()
                   .AddEventing()

                   .ConfigureContainer <ContainerBuilder>((context, builder) => { })

                   .ConfigureServices((context, services) =>
            {
                services.AddControllers().AddApplicationPart(Assembly.GetEntryAssembly()).AddControllersAsServices();
            })

                   .ConfigureWebHostDefaults(builder => builder.Configure((context, app) =>
            {
                if (context.HostingEnvironment.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }

                app.AddErrorHandling();
                app.UseRouting();

                app.UseAuthentication();
                app.UseAuthorization();

                var appSettings = new ApplicationOptions();
                context.Configuration.Bind("Application", appSettings);

                var ssoSettings = new SingleSignOnOptions();
                context.Configuration.Bind("SingleSignOn", ssoSettings);

                var openApiSettings = new OpenApiDocumentationOptions()
                {
                    Path = "/swagger"
                };

                app.AddOpenApiDocumentation(appSettings, ssoSettings, openApiSettings);

                app.AddReadiness();
                app.AddLiveness();

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                    endpoints.MapHttpRequestHandler("api");
                });
            }))
                   );
        }
示例#5
0
        public static IApplicationBuilder AddOpenApiDocumentation(this IApplicationBuilder app, ApplicationOptions appSettings, SingleSignOnOptions ssoSettings, OpenApiDocumentationOptions openApiSettings)
        {
            Log.Verbose("Configured OpenAPI on path {documentPath}", openApiSettings.Path);

            return(app

                   .UseOpenApi(settings => { settings.DocumentName = settings.DocumentName; })

                   .UseSwaggerUi3(settings =>
            {
                settings.Path = openApiSettings.Path;
                settings.OAuth2Client = new OAuth2ClientSettings
                {
                    AppName = appSettings.Name,
                    ClientId = ssoSettings.ClientId
                };
                settings.OAuth2Client.AdditionalQueryStringParameters.Add("nonce", "123456");
            }));
        }