public static IMvcBuilder AddCsvSerializerFormatters(this IMvcBuilder builder, CsvFormatterOptions csvFormatterOptions) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat("csv", new MediaTypeHeaderValue("text/csv"))); if (csvFormatterOptions == null) { csvFormatterOptions = new CsvFormatterOptions(); } if (string.IsNullOrEmpty(csvFormatterOptions.CsvDelimiter)) { throw new ArgumentException("CsvDelimiter cannot be empty"); } builder.AddMvcOptions(options => options.InputFormatters.Add(new CsvInputFormatter(csvFormatterOptions))); builder.AddMvcOptions(options => options.OutputFormatters.Add(new CsvOutputFormatter(csvFormatterOptions))); return(builder); }
public static IMvcBuilder AddMessagePackFormatters(this IMvcBuilder builder, Action <MessagePackFormatterOptions> messagePackFormatterOptionsConfiguration) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var messagePackFormatterOptions = new MessagePackFormatterOptions(); messagePackFormatterOptionsConfiguration?.Invoke(messagePackFormatterOptions); foreach (var extension in messagePackFormatterOptions.SupportedExtensions) { foreach (var contentType in messagePackFormatterOptions.SupportedContentTypes) { builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat(extension, new MediaTypeHeaderValue(contentType))); } } builder.AddMvcOptions(options => { options.ModelMetadataDetailsProviders.Add(new DefaultBindingMetadataProvider()); options.ModelMetadataDetailsProviders.Add(new DefaultValidationMetadataProvider()); }); builder.AddMvcOptions(options => options.OutputFormatters.Add(new MessagePackOutputFormatter(messagePackFormatterOptions))); return(builder); }
/// <summary> /// Add the Certificate endpoints feature to MVC. /// </summary> /// <param name="mvcBuilder">An interface for configuring MVC services.</param> /// <param name="configureAction">Delegate for configuring options for certificate endpoints feature.</param> /// <returns></returns> public static IMvcBuilder AddCertificateEndpoints(this IMvcBuilder mvcBuilder, Action <CertificateEndpointsOptions> configureAction = null) { mvcBuilder.ConfigureApplicationPartManager(apm => apm.FeatureProviders.Add(new CertificatesFeatureProvider())); mvcBuilder.AddFormatterMappings(mappings => { mappings.SetMediaTypeMappingForFormat("crt", "application/x-x509-user-cert"); // The CRT extension is used for certificates. The certificates may be encoded as binary DER or as ASCII PEM. mappings.SetMediaTypeMappingForFormat("cer", "application/pkix-cert"); // Alternate form of .crt (Microsoft Convention). You can use MS to convert .crt to .cer (.both DER encoded .cer, or base64[PEM] encoded .cer) mappings.SetMediaTypeMappingForFormat("key", "application/pkcs8"); // The KEY extension is used both for public and private PKCS#8 keys. The keys may be encoded as binary DER or as ASCII PEM. mappings.SetMediaTypeMappingForFormat("pfx", "application/x-pkcs12"); // PFX. }); mvcBuilder.AddMvcOptions(mvc => { mvc.OutputFormatters.Add(new CertificateOutputFormatter()); }); var options = new CertificateEndpointsOptions { IssuerDomain = "www.example.com", PfxPassphrase = "???" }; options.Services = mvcBuilder.Services; configureAction?.Invoke(options); options.Services = null; if (options.Path == null) { var serviceProvider = mvcBuilder.Services.BuildServiceProvider(); var hostingEnvironment = serviceProvider.GetRequiredService <IWebHostEnvironment>(); options.Path = Path.Combine(hostingEnvironment.ContentRootPath, "App_Data", "certs"); } mvcBuilder.Services.AddSingleton(options); if (!Directory.Exists(options.Path)) { Directory.CreateDirectory(options.Path); } if (!File.Exists(Path.Combine(options.Path, "ca.pfx"))) { var serviceProvider = mvcBuilder.Services.BuildServiceProvider(); var manager = new CertificateManager(); var issuingCert = manager.CreateRootCACertificate(options.IssuerDomain); var certBase64 = issuingCert.ExportToPEM(); var pfxBytes = issuingCert.Export(X509ContentType.Pfx, options.PfxPassphrase); File.WriteAllBytes(Path.Combine(options.Path, "ca.pfx"), pfxBytes); File.WriteAllText(Path.Combine(options.Path, "ca.cer"), certBase64); var store = serviceProvider.GetService <ICertificatesStore>(); var taskFactory = new TaskFactory(); var addTask = store.Add(issuingCert, null); taskFactory.StartNew(() => addTask).Unwrap().GetAwaiter().GetResult(); } return(mvcBuilder); }
public static IMvcBuilder AddProtoBuf(this IMvcBuilder builder, Action <ProtoBufFormatterOptions>?setupAction = null) { var options = new ProtoBufFormatterOptions(); setupAction?.Invoke(options); foreach (var extension in options.SupportedExtensions) { foreach (var contentType in options.SupportedContentTypes) { builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat(extension, new MediaTypeHeaderValue(contentType))); } } builder.Services.AddOptions <MvcOptions>() .Configure <ILoggerFactory>((mvcOptions, loggerFactory) => ConfigureMvcOptions(options, mvcOptions, loggerFactory)); return(builder); }
public static IMvcBuilder AddMessagePackFormatters(this IMvcBuilder builder, Action <MessagePackFormatterOptions> messagePackFormatterOptionsConfiguration) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var messagePackFormatterOptions = new MessagePackFormatterOptions(); messagePackFormatterOptionsConfiguration?.Invoke(messagePackFormatterOptions); messagePackFormatterOptions.Options.WithResolver(messagePackFormatterOptions.FormatterResolver); foreach (var extension in messagePackFormatterOptions.SupportedExtensions) { foreach (var contentType in messagePackFormatterOptions.SupportedContentTypes) { builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat(extension, new MediaTypeHeaderValue(contentType))); } } //builder.AddMvcOptions(options => options.InputFormatters.Add(new MessagePackInputFormatter(messagePackFormatterOptions))); //builder.AddMvcOptions(options => options.OutputFormatters.Add(new MessagePackOutputFormatter(messagePackFormatterOptions))); builder.AddMvcOptions(options => { options.InputFormatters.Clear(); options.InputFormatters.Add(new MessagePackInputFormatter(messagePackFormatterOptions)); }); builder.AddMvcOptions(options => { options.OutputFormatters.Clear(); options.OutputFormatters.Add(new MessagePackOutputFormatter(messagePackFormatterOptions)); }); return(builder); }
public static IMvcBuilder AddProtobufFormatters(this IMvcBuilder builder, Action <ProtobufFormatterOptions> protobufFormatterOptionsConfiguration) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var protobufFormatterOptions = new ProtobufFormatterOptions(); protobufFormatterOptionsConfiguration?.Invoke(protobufFormatterOptions); foreach (var extension in protobufFormatterOptions.SupportedExtensions) { foreach (var contentType in protobufFormatterOptions.SupportedContentTypes) { builder.AddFormatterMappings(m => m.SetMediaTypeMappingForFormat(extension, new MediaTypeHeaderValue(contentType))); } } builder.AddMvcOptions(options => options.InputFormatters.Add(new ProtobufInputFormatter(protobufFormatterOptions))); builder.AddMvcOptions(options => options.OutputFormatters.Add(new ProtobufOutputFormatter(protobufFormatterOptions))); return(builder); }