Startup.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Text.Encodings.Web;
  2. using System.Text.Unicode;
  3. using Essensoft.AspNetCore.Payment.Alipay;
  4. using Essensoft.AspNetCore.Payment.JDPay;
  5. using Essensoft.AspNetCore.Payment.LianLianPay;
  6. using Essensoft.AspNetCore.Payment.QPay;
  7. using Essensoft.AspNetCore.Payment.UnionPay;
  8. using Essensoft.AspNetCore.Payment.WeChatPay;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.Extensions.Logging;
  16. namespace WebApplicationSample
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. }
  24. public IConfiguration Configuration { get; }
  25. // This method gets called by the runtime. Use this method to add services to the container.
  26. public void ConfigureServices(IServiceCollection services)
  27. {
  28. services.Configure<CookiePolicyOptions>(options =>
  29. {
  30. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  31. options.CheckConsentNeeded = context => true;
  32. options.MinimumSameSitePolicy = SameSiteMode.None;
  33. });
  34. // 引入HttpClient
  35. services.AddHttpClient();
  36. //引入HttpClient API证书的使用(仅QPay / WeChatPay的部分API使用到)。
  37. //services.AddHttpClient("qpayCertificateName").ConfigurePrimaryHttpMessageHandler(() =>
  38. //{
  39. // var certificate = new X509Certificate2("", "", X509KeyStorageFlags.MachineKeySet);
  40. // var handler = new HttpClientHandler();
  41. // handler.ClientCertificates.Add(certificate);
  42. // return handler;
  43. //});
  44. //services.AddHttpClient("wechatpayCertificateName").ConfigurePrimaryHttpMessageHandler(() =>
  45. //{
  46. // var certificate = new X509Certificate2("", "", X509KeyStorageFlags.MachineKeySet);
  47. // var handler = new HttpClientHandler();
  48. // handler.ClientCertificates.Add(certificate);
  49. // return handler;
  50. //});
  51. // 引入Payment 依赖注入
  52. services.AddAlipay();
  53. services.AddJDPay();
  54. services.AddQPay();
  55. services.AddUnionPay();
  56. services.AddWeChatPay();
  57. services.AddLianLianPay();
  58. // 在 appsettings.json 中 配置选项
  59. services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
  60. services.Configure<JDPayOptions>(Configuration.GetSection("JDPay"));
  61. services.Configure<QPayOptions>(Configuration.GetSection("QPay"));
  62. services.Configure<UnionPayOptions>(Configuration.GetSection("UnionPay"));
  63. services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
  64. services.Configure<LianLianPayOptions>(Configuration.GetSection("LianLianPay"));
  65. services.AddWebEncoders(opt =>
  66. {
  67. opt.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
  68. });
  69. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  70. }
  71. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  72. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  73. {
  74. if (env.IsDevelopment())
  75. {
  76. app.UseDeveloperExceptionPage();
  77. }
  78. else
  79. {
  80. app.UseExceptionHandler("/Home/Error");
  81. app.UseHsts();
  82. }
  83. app.UseStaticFiles();
  84. app.UseCookiePolicy();
  85. app.UseMvc(routes =>
  86. {
  87. routes.MapRoute(
  88. name: "default",
  89. template: "{controller=Home}/{action=Index}/{id?}");
  90. });
  91. }
  92. }
  93. }