| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright (c) .NET Foundation. All rights reserved.
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- using System;
- using System.Globalization;
- using System.Text.Encodings.Web;
- using Microsoft.AspNetCore.Authentication.OAuth;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.DataProtection;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Microsoft.AspNetCore.Authentication;
- namespace Winsoft.GOV.XF.WXCore.OAuth
- {
- /// <summary>
- /// An ASP.NET Core middleware for authenticating users using WeChat.
- /// </summary>
- public class WeChatMiddleware : OAuthMiddleware<WeChatOptions>
- {
- /// <summary>
- /// Initializes a new <see cref="WeChatMiddleware"/>.
- /// </summary>
- /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
- /// <param name="dataProtectionProvider"></param>
- /// <param name="loggerFactory"></param>
- /// <param name="encoder"></param>
- /// <param name="sharedOptions"></param>
- /// <param name="options">Configuration options for the middleware.</param>
- public WeChatMiddleware(
- RequestDelegate next,
- IDataProtectionProvider dataProtectionProvider,
- ILoggerFactory loggerFactory,
- UrlEncoder encoder,
- IOptions<SharedAuthenticationOptions> sharedOptions,
- IOptions<WeChatOptions> options)
- : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
- {
- if (next == null)
- {
- throw new ArgumentNullException(nameof(next));
- }
- if (dataProtectionProvider == null)
- {
- throw new ArgumentNullException(nameof(dataProtectionProvider));
- }
- if (loggerFactory == null)
- {
- throw new ArgumentNullException(nameof(loggerFactory));
- }
- if (encoder == null)
- {
- throw new ArgumentNullException(nameof(encoder));
- }
- if (sharedOptions == null)
- {
- throw new ArgumentNullException(nameof(sharedOptions));
- }
- if (options == null)
- {
- throw new ArgumentNullException(nameof(options));
- }
- if (string.IsNullOrEmpty(Options.AppId))
- {
- throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppId)));
- }
- if (string.IsNullOrEmpty(Options.AppSecret))
- {
- throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppSecret)));
- }
- }
- /// <summary>
- /// Provides the <see cref="AuthenticationHandler{T}"/> object for processing authentication-related requests.
- /// </summary>
- /// <returns>An <see cref="AuthenticationHandler{T}"/> configured with the <see cref="WeChatOptions"/> supplied to the constructor.</returns>
- protected override AuthenticationHandler<WeChatOptions> CreateHandler()
- {
- return new WeChatHandler(Backchannel);
- }
- }
- }
|