WeChatMiddleware.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Globalization;
  5. using System.Text.Encodings.Web;
  6. using Microsoft.AspNetCore.Authentication.OAuth;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.DataProtection;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Extensions.Options;
  12. using Microsoft.AspNetCore.Authentication;
  13. namespace Winsoft.GOV.XF.WXCore.OAuth
  14. {
  15. /// <summary>
  16. /// An ASP.NET Core middleware for authenticating users using WeChat.
  17. /// </summary>
  18. public class WeChatMiddleware : OAuthMiddleware<WeChatOptions>
  19. {
  20. /// <summary>
  21. /// Initializes a new <see cref="WeChatMiddleware"/>.
  22. /// </summary>
  23. /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
  24. /// <param name="dataProtectionProvider"></param>
  25. /// <param name="loggerFactory"></param>
  26. /// <param name="encoder"></param>
  27. /// <param name="sharedOptions"></param>
  28. /// <param name="options">Configuration options for the middleware.</param>
  29. public WeChatMiddleware(
  30. RequestDelegate next,
  31. IDataProtectionProvider dataProtectionProvider,
  32. ILoggerFactory loggerFactory,
  33. UrlEncoder encoder,
  34. IOptions<SharedAuthenticationOptions> sharedOptions,
  35. IOptions<WeChatOptions> options)
  36. : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
  37. {
  38. if (next == null)
  39. {
  40. throw new ArgumentNullException(nameof(next));
  41. }
  42. if (dataProtectionProvider == null)
  43. {
  44. throw new ArgumentNullException(nameof(dataProtectionProvider));
  45. }
  46. if (loggerFactory == null)
  47. {
  48. throw new ArgumentNullException(nameof(loggerFactory));
  49. }
  50. if (encoder == null)
  51. {
  52. throw new ArgumentNullException(nameof(encoder));
  53. }
  54. if (sharedOptions == null)
  55. {
  56. throw new ArgumentNullException(nameof(sharedOptions));
  57. }
  58. if (options == null)
  59. {
  60. throw new ArgumentNullException(nameof(options));
  61. }
  62. if (string.IsNullOrEmpty(Options.AppId))
  63. {
  64. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppId)));
  65. }
  66. if (string.IsNullOrEmpty(Options.AppSecret))
  67. {
  68. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppSecret)));
  69. }
  70. }
  71. /// <summary>
  72. /// Provides the <see cref="AuthenticationHandler{T}"/> object for processing authentication-related requests.
  73. /// </summary>
  74. /// <returns>An <see cref="AuthenticationHandler{T}"/> configured with the <see cref="WeChatOptions"/> supplied to the constructor.</returns>
  75. protected override AuthenticationHandler<WeChatOptions> CreateHandler()
  76. {
  77. return new WeChatHandler(Backchannel);
  78. }
  79. }
  80. }