예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(o =>
            {
                o.ResourcesPath = "Resources";
            });
            services.Configure <RequestLocalizationOptions>(o => {
                o.SupportedUICultures = new List <CultureInfo>
                {
                    new CultureInfo("de"),
                    new CultureInfo("bs"),
                    new CultureInfo("es"),
                    new CultureInfo("en"),
                    new CultureInfo("fr-FR"),
                };
                o.DefaultRequestCulture      = new Microsoft.AspNetCore.Localization.RequestCulture("es");
                o.FallBackToParentCultures   = true;
                o.FallBackToParentUICultures = true;
                o.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
                o.RequestCultureProviders.Insert(1, new CountryDomainRequestCultureProvider());
            });
            IMvcCoreBuilder mvcCoreBuilder = services.AddMvcCore();

            mvcCoreBuilder.AddJsonFormatters();
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options =>
            {
                options.ResourcesPath = "Resources";
            });

            services.Configure <RequestLocalizationOptions>(options =>
            {
                options.SupportedUICultures = new List <CultureInfo>()
                {
                    new CultureInfo("ba"),
                    new CultureInfo("bs"),
                    new CultureInfo("de"),
                };

                options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("bs");
            });

            IMvcCoreBuilder mvcCoreBuilder = services.AddMvcCore();

            mvcCoreBuilder.AddJsonFormatters();

            // services.AddMvc();
        }
예제 #3
0
        public static void ConfigureCoreMvc(IMvcCoreBuilder builder)
        {
            builder.AddAuthorization(options =>
            {
                options.AddPolicy("ValidateClaims", policyBuilder =>
                {
                    policyBuilder.RequireAuthenticatedUser();
                    policyBuilder.RequireAssertion(context =>
                    {
                        var principal           = context.User;
                        var nameIdentifierClaim = principal.FindFirst(ClaimTypes.NameIdentifier);

                        if (nameIdentifierClaim == null ||
                            nameIdentifierClaim.Value != "1" ||
                            nameIdentifierClaim.ValueType != ClaimValueTypes.Integer32 ||
                            nameIdentifierClaim.Issuer != "TestIssuer" ||
                            nameIdentifierClaim.OriginalIssuer != "OriginalTestIssuer")
                        {
                            return(false);
                        }

                        return(true);
                    });
                });
            });
            builder.AddJsonFormatters(options =>
            {
                options.NullValueHandling = NullValueHandling.Ignore;
            });
        }
예제 #4
0
        private static IMvcCoreBuilder ConfigureJson(this IMvcCoreBuilder builder)
        {
            builder.AddFormatterMappings();

            builder.AddJsonFormatters(f =>
            {
                f.Formatting            = Formatting.Indented;
                f.NullValueHandling     = NullValueHandling.Ignore;
                f.MissingMemberHandling = MissingMemberHandling.Ignore;
            });

            builder.AddJsonOptions(options =>
            {
                options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;

                options.SerializerSettings.Converters.Add(new StringEnumConverter());
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
                    Converters = new List <JsonConverter> {
                        new StringEnumConverter()
                    },
                    DateParseHandling = DateParseHandling.DateTimeOffset,
                    ContractResolver  = new CamelCasePropertyNamesContractResolver()
                };
            });

            return(builder);
        }
예제 #5
0
        public static IMvcCoreBuilder AddJson(this IMvcCoreBuilder mvc)
        {
#if NETCOREAPP2_2
            return(mvc.AddJsonFormatters(json => json.NullValueHandling = NullValueHandling.Ignore));
#else
            return(mvc.AddJsonOptions(json => { json.JsonSerializerOptions.IgnoreNullValues = true; }));
#endif
        }
예제 #6
0
 public static void ConfigureCoreMvc(IMvcCoreBuilder builder)
 {
     builder.AddAuthorization();
     builder.AddJsonFormatters(options =>
     {
         options.NullValueHandling = NullValueHandling.Ignore;
     });
 }
예제 #7
0
 public static void ConfigureJson(this IMvcCoreBuilder mvc)
 {
     mvc.AddJsonFormatters();
     mvc.AddJsonOptions(options => {
         options.SerializerSettings.Converters.Add(new StringEnumConverter {
             CamelCaseText = true
         });
     });
 }
예제 #8
0
        /// <summary>
        /// <para>
        /// Add AzureContainerRegistry WebHook configuration and services to the specified <paramref name="builder"/>. See
        /// <see href="https://docs.microsoft.com/en-us/azure/container-registry/container-registry-webhook-reference"/> for additional details about AzureContainerRegistry WebHook requests.
        /// </para>
        /// <para>
        /// The '<c>WebHooks:AzureContainerRegistry:SecretKey:default</c>' configuration value contains the secret key for AzureContainerRegistry
        /// WebHook URIs of the form '<c>https://{host}/api/webhooks/incoming/azurecontainerregistry</c>'.
        /// '<c>WebHooks:AzureContainerRegistry:SecretKey:{id}</c>' configuration values contain secret keys for AzureContainerRegistry WebHook URIs of
        /// the form '<c>https://{host}/api/webhooks/incoming/azurecontainerregistry/{id}</c>'.
        /// </para>
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddAzureContainerRegistryWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            AzureContainerRegistryServiceCollectionSetup.AddAzureContainerRegistryServices(builder.Services);

            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks());
        }
 public static IMvcCoreBuilder AddCustomJsonSettings(this IMvcCoreBuilder mvcBuilder)
 {
     return(mvcBuilder
            .AddJsonFormatters(settings =>
     {
         settings.NullValueHandling = NullValueHandling.Ignore;
         settings.Converters.Add(
             new IsoDateTimeConverter
         {
             DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
         });
     }));
 }
        /// <summary>
        /// <para>
        /// Add Dropbox WebHook configuration and services to the specified <paramref name="builder"/>. See
        /// <see href="https://www.dropbox.com/developers/webhooks/docs"/> for additional details about Dropbox WebHook
        /// requests.
        /// </para>
        /// <para>
        /// The '<c>WebHooks:Dropbox:SecretKey:default</c>' configuration value contains the secret key for Dropbox
        /// WebHook URIs of the form '<c>https://{host}/api/webhooks/incoming/dropbox</c>'.
        /// '<c>WebHooks:Dropbox:SecretKey:{id}</c>' configuration values contain secret keys for Dropbox WebHook URIs
        /// of the form '<c>https://{host}/api/webhooks/incoming/dropbox/{id}</c>'.
        /// </para>
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddDropboxWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            DropboxServiceCollectionSetup.AddDropboxServices(builder.Services);

            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks());
        }
        /// <summary>
        /// Add Dynamic CRM WebHook configuration and services to the specified <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddDynamicsCRMWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <IWebHookMetadata, DynamicsCRMMetadata>());

            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks());
        }
        /// <summary>
        /// <para>
        /// Add Fitbit WebHook configuration and services to the specified <paramref name="builder"/>.
        /// </para>
        /// <para>
        /// The '<c>WebHooks:fitbit:SecretKey:{id}</c>' configuration value contains the secret key for
        /// Fitbit URIs of the form '<c>https://{host}/api/webhooks/incoming/fitbit/{id}?verify={secret key}</c>'.
        /// </para>
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <param name="setupAction">
        /// The setup action for <see cref="FitbitWebhookReceiverOptions"/> to determine how the receivers will process requests.
        /// </param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddFitbitWebHooks(this IMvcCoreBuilder builder, Action <FitbitWebhookReceiverOptions> setupAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            FitbitServiceCollectionSetup.AddFitbitServices(builder.Services, setupAction);

            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks());
        }
        /// <summary>
        /// <para>
        /// Add Slack WebHook configuration and services to the specified <paramref name="builder"/>. See
        /// <see href="https://api.slack.com/custom-integrations/outgoing-webhooks"/> for additional details about
        /// Slack WebHook requests.
        /// </para>
        /// <para>
        /// The '<c>WebHooks:Slack:SecretKey:default</c>' configuration value contains the secret key for Slack WebHook
        /// URIs of the form '<c>https://{host}/api/webhooks/incoming/slack</c>'.
        /// '<c>WebHooks:Slack:SecretKey:{id}</c>' configuration values contain secret keys for Slack WebHook URIs of
        /// the form '<c>https://{host}/api/webhooks/incoming/slack/{id}</c>'.
        /// </para>
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddSlackWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            SlackServiceCollectionSetup.AddSlackServices(builder.Services);

            // While requests contain HTML form URL-encoded data, responses are JSON.
            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks());
        }
        /// <summary>
        /// Add WebSub WebHook configuration and services to the specified <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddWebSubWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            WebSubServiceCollectionSetup.AddWebSubServices(builder.Services);

            return builder
                .AddJsonFormatters()
                .AddXmlSerializerFormatters()
                .AddWebHooks();
        }
        /// <summary>
        /// Add GitHub WebHook configuration and services to the specified <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddGitHubWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <IWebHookMetadata, GitHubMetadata>());

            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks()
                   .AddSingletonFilter <GitHubVerifySignatureFilter>(WebHookSecurityFilter.Order));
        }
예제 #16
0
        private static IMvcBuilder AddWebApi(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            IMvcCoreBuilder builder = services.AddMvcCore();

            builder.AddJsonFormatters();
            builder.AddApiExplorer();
            builder.AddCors();

            return(new MvcBuilder(builder.Services, builder.PartManager));
        }
예제 #17
0
 public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder,
                                                    IHostingEnvironment hostingEnvironment)
 {
     return(builder
            .AddJsonFormatters(settings =>
     {
         if (hostingEnvironment.IsDevelopment())
         {
             settings.Formatting = Formatting.Indented;
         }
     })
            .AddJsonOptions(options =>
     {
         options.SerializerSettings.ContractResolver = new DefaultContractResolver();
     }));
 }
예제 #18
0
        public static IMvcCoreBuilder AddGitLfs(this IMvcCoreBuilder builder)
        {
            builder.AddJsonFormatters();
            builder.AddApplicationPart(typeof(LfsConstants).GetTypeInfo().Assembly);
            builder.AddMvcOptions(options =>
            {
                options.Filters.Add(new ProducesAttribute(LfsConstants.LfsMediaType.MediaType.Buffer));
                options.Filters.Add(new TypeFilterAttribute(typeof(BasicAuthFilter)));

                JsonOutputFormatter jsonOutput = options.OutputFormatters.OfType <JsonOutputFormatter>().First();
                jsonOutput.SupportedMediaTypes.Add(LfsConstants.LfsMediaType);

                JsonInputFormatter jsonInput = options.InputFormatters.OfType <JsonInputFormatter>().First();
                jsonInput.SupportedMediaTypes.Add(LfsConstants.LfsMediaType);
            });
            return(builder);
        }
        /// <summary>
        /// Add Slack WebHook configuration and services to the specified <paramref name="builder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IMvcCoreBuilder" /> to configure.</param>
        /// <returns>The <paramref name="builder"/>.</returns>
        public static IMvcCoreBuilder AddSlackWebHooks(this IMvcCoreBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var services = builder.Services;

            services.TryAddEnumerable(ServiceDescriptor.Singleton <IWebHookMetadata, SlackMetadata>());

            // While requests contain HTTP form data, responses are JSON.
            return(builder
                   .AddJsonFormatters()
                   .AddWebHooks(OptionsSetupAction)
                   .AddSingletonFilter <SlackVerifyTokenFilter>(WebHookSecurityFilter.Order));
        }
예제 #20
0
        private IWebHostBuilder SetupWebHost(StringWriter testOutput)
        {
            var hostBuilder = new WebHostBuilder();

            // Configure services
#if ASPNETCORE2_0
            hostBuilder.UseLogJam((LogManagerConfig config, WebHostBuilderContext hostBuilderContext) =>
#else
            hostBuilder.UseLogJam((LogManagerConfig config, IServiceProvider serviceProvider) =>
#endif
            {
                config.UseTestOutput(_testOutput);
                config.UseTextWriter(testOutput);
            });

            hostBuilder.ConfigureServices(serviceCollection =>
            {
                IMvcCoreBuilder mvcCoreBuilder = serviceCollection.AddMvcCore();
                mvcCoreBuilder.AddJsonFormatters();
#if ASPNETCORE2_0
                serviceCollection.Configure <LoggerFilterOptions>(filterOptions => filterOptions.MinLevel = LogLevel.Trace);
#else
                serviceCollection.Configure <FilterLoggerSettings>(filterSettings => filterSettings.Add("Default", LogLevel.Trace));
#endif
            });

            // Configure webapp
            hostBuilder.Configure(appBuilder =>
            {
                appBuilder.UseStatusCodePages();
                appBuilder.UseExceptionHandler(new ExceptionHandlerOptions()
                {
                    ExceptionHandler = async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("Unhandled exception");
                    }
                });
                appBuilder.UseMvc();
            });

            return(hostBuilder);
        }
예제 #21
0
    internal static IMvcCoreBuilder AddNewtonsoftJson(this IMvcCoreBuilder mvc, Action <MvcJsonOptions> setupAction)
    {
        if (mvc == null)
        {
            throw new ArgumentNullException(nameof(mvc));
        }

        if (setupAction == null)
        {
            throw new ArgumentNullException(nameof(setupAction));
        }

        // Add JSON formatters that will be used as default ones if no specific formatters are asked for
        mvc.AddJsonFormatters();

        mvc.AddJsonOptions(setupAction);

        return(mvc);
    }
예제 #22
0
        /// <summary>
        /// Adds WebApi services to the specified <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the WebApi services.</returns>
        public static IMvcBuilder AddWebApi(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException("services");
            }
            IMvcCoreBuilder mvcCoreBuilder = services.AddMvcCore();

            mvcCoreBuilder.AddApiExplorer();
            mvcCoreBuilder.AddAuthorization();
            mvcCoreBuilder.AddFormatterMappings();
            mvcCoreBuilder.AddDataAnnotations();
            mvcCoreBuilder.AddJsonFormatters();
            mvcCoreBuilder.AddCors();

            //-MvcServiceCollectionExtensions.AddDefaultFrameworkParts(mvcCoreBuilder.PartManager);
            //-mvcCoreBuilder.AddViews();
            //-mvcCoreBuilder.AddRazorViewEngine();
            //-mvcCoreBuilder.AddRazorPages();
            //-mvcCoreBuilder.AddCacheTagHelper();

            return(new MvcBuilder(mvcCoreBuilder.Services, mvcCoreBuilder.PartManager));
        }
예제 #23
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddResponseCompression();
            services.AddCors(builder => builder.AddDefaultPolicy(policyBuilder => policyBuilder.AllowAnyOrigin()));
            IMvcCoreBuilder mvc = services.AddMvcCore();

            mvc.AddApiExplorer();
            mvc.SetCompatibilityVersion(CompatibilityVersion.Latest);
            mvc.AddFormatterMappings();
            mvc.AddJsonFormatters();
            mvc.AddJsonOptions(
                options => {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                options.SerializerSettings.Formatting       = Formatting.Indented;
            }
                );

            services.AddSignalR();
            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Version     = "v1",
                    Title       = "Home assistant API",
                    Description = $"Documentation for {Core.AssistantName} api endpoints.",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Arun Prakash",
                        Email = "*****@*****.**",
                        Url   = new Uri("https://github.com/SynergYFTW/HomeAssistant")
                    },
                    License = new OpenApiLicense()
                    {
                        Name = "MIT License",
                        Url  = new Uri("https://github.com/SynergYFTW/HomeAssistant/blob/master/LICENSE")
                    }
                });
            });
        }
예제 #24
0
 public static IMvcCoreBuilder AddJsonMergePatch(this IMvcCoreBuilder builder, Action <JsonMergePatchOptions> configure = null)
 {
     builder.AddJsonFormatters();
     builder.Services.AddJsonMergePatch(configure);
     return(builder);
 }