123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #region Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- either express or implied. See the License for the specific language governing permissions
- and limitations under the License.
- Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
- ----------------------------------------------------------------*/
- #endregion Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright (C) 2019 Senparc
- 文件名:OAuthContainer.cs
- 文件功能描述:用户OAuth容器,用于自动管理OAuth的AccessToken,如果过期会重新获取
- 创建标识:Senparc - 20160801
- 修改标识:Senparc - 20160803
- 修改描述:v14.2.3 使用ApiUtility.GetExpireTime()方法处理过期
-
- 修改标识:Senparc - 20160804
- 修改描述:v14.2.4 增加TryGetOAuthAccessTokenAsync,GetOAuthAccessTokenAsync,GetOAuthAccessTokenResultAsync的异步方法
- 修改标识:Senparc - 20160808
- 修改描述:v14.3.0 删除 ItemCollection 属性,直接使用ContainerBag加入到缓存
-
- 修改标识:Senparc - 20160813
- 修改描述:v14.3.4 添加TryReRegister()方法,处理分布式缓存重启(丢失)的情况
-
- 修改标识:Senparc - 20160813
- 修改描述:v14.3.6 完善getNewToken参数传递
- 修改标识:Senparc - 20180614
- 修改描述:CO2NET v0.1.0 ContainerBag 取消属性变动通知机制,使用手动更新缓存
- 修改标识:Senparc - 20180707
- 修改描述:v15.0.9 Container 的 Register() 的微信参数自动添加到 Config.SenparcWeixinSetting.Items 下
- 修改标识:Senparc - 20181226
- 修改描述:v16.6.2 修改 DateTime 为 DateTimeOffset
- ----------------------------------------------------------------*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Senparc.Weixin.Cache;
- using Senparc.Weixin.Containers;
- using Senparc.Weixin.Exceptions;
- using Senparc.Weixin.MP.Entities;
- using Senparc.CO2NET.CacheUtility;
- using Senparc.Weixin.MP.AdvancedAPIs;
- using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
- using Senparc.Weixin.MP.CommonAPIs;
- using Senparc.Weixin.Utilities.WeixinUtility;
- using Senparc.CO2NET.Extensions;
- namespace Senparc.Weixin.MP.Containers
- {
- /// <summary>
- /// OAuth包
- /// </summary>
- [Serializable]
- public class OAuthAccessTokenBag : BaseContainerBag, IBaseContainerBag_AppId
- {
- public string AppId { get; set; }
- // {
- // get { return _appId; }
- //#if NET35 || NET40
- // set { this.SetContainerProperty(ref _appId, value, "AppId"); }
- //#else
- // set { this.SetContainerProperty(ref _appId, value); }
- //#endif
- // }
- public string AppSecret { get; set; }
- // {
- // get { return _appSecret; }
- //#if NET35 || NET40
- // set { this.SetContainerProperty(ref _appSecret, value, "AppSecret"); }
- //#else
- // set { this.SetContainerProperty(ref _appSecret, value); }
- //#endif
- // }
- public OAuthAccessTokenResult OAuthAccessTokenResult { get; set; }
- // {
- // get { return _oAuthAccessTokenResult; }
- //#if NET35 || NET40
- // set { this.SetContainerProperty(ref _oAuthAccessTokenResult, value, "OAuthAccessTokenResult"); }
- //#else
- // set { this.SetContainerProperty(ref _oAuthAccessTokenResult, value); }
- //#endif
- // }
- public DateTimeOffset OAuthAccessTokenExpireTime { get; set; }
- // {
- // get { return _oAuthAccessTokenExpireTime; }
- //#if NET35 || NET40
- // set { this.SetContainerProperty(ref _oAuthAccessTokenExpireTime, value, "OAuthAccessTokenExpireTime"); }
- //#else
- // set { this.SetContainerProperty(ref _oAuthAccessTokenExpireTime, value); }
- //#endif
- // }
- /// <summary>
- /// 只针对这个AppId的锁
- /// </summary>
- internal object Lock = new object();
- //private DateTimeOffset _oAuthAccessTokenExpireTime;
- //private OAuthAccessTokenResult _oAuthAccessTokenResult;
- //private string _appSecret;
- //private string _appId;
- }
- /// <summary>
- /// 用户OAuth容器,用于自动管理OAuth的AccessToken,如果过期会重新获取(测试中,暂时别用)
- /// </summary>
- public class OAuthAccessTokenContainer : BaseContainer<OAuthAccessTokenBag>
- {
- const string LockResourceName = "MP.OAuthAccessTokenContainer";
- #region 同步方法
- //static Dictionary<string, JsApiTicketBag> JsApiTicketCollection =
- // new Dictionary<string, JsApiTicketBag>(StringComparer.OrdinalIgnoreCase);
- /// <summary>
- /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <param name="name">标记JsApiTicket名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
- /// 此接口不提供异步方法
- public static void Register(string appId, string appSecret, string name = null)
- {
- RegisterFunc = () =>
- {
- //using (FlushCache.CreateInstance())
- //{
- var bag = new OAuthAccessTokenBag()
- {
- Name = name,
- AppId = appId,
- AppSecret = appSecret,
- OAuthAccessTokenExpireTime = DateTimeOffset.MinValue,
- OAuthAccessTokenResult = new OAuthAccessTokenResult()
- };
- Update(appId, bag, null);
- return bag;
- //}
- };
- RegisterFunc();
- if (!name.IsNullOrEmpty())
- {
- Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinAppId = appId;
- Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinAppSecret = appSecret;
- }
- }
- #region OAuthAccessToken
- /// <summary>
- /// 使用完整的应用凭证获取Ticket,如果不存在将自动注册
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken"></param>
- /// <returns></returns>
- public static string TryGetOAuthAccessToken(string appId, string appSecret, string code, bool getNewToken = false)
- {
- if (!CheckRegistered(appId) || getNewToken)
- {
- Register(appId, appSecret);
- }
- return GetOAuthAccessToken(appId, code, getNewToken);
- }
- /// <summary>
- /// 获取可用Ticket
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
- /// <returns></returns>
- public static string GetOAuthAccessToken(string appId, string code, bool getNewToken = false)
- {
- return GetOAuthAccessTokenResult(appId, code, getNewToken).access_token;
- }
- /// <summary>
- /// 获取可用Ticket
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
- /// <returns></returns>
- public static OAuthAccessTokenResult GetOAuthAccessTokenResult(string appId, string code, bool getNewToken = false)
- {
- if (!CheckRegistered(appId))
- {
- throw new UnRegisterAppIdException(null, "此appId尚未注册,请先使用OAuthAccessTokenContainer.Register完成注册(全局执行一次即可)!");
- }
- var oAuthAccessTokenBag = TryGetItem(appId);
- using (Cache.BeginCacheLock(LockResourceName, appId))//同步锁
- {
- if (getNewToken || oAuthAccessTokenBag.OAuthAccessTokenExpireTime <= SystemTime.Now)
- {
- //已过期,重新获取
- oAuthAccessTokenBag.OAuthAccessTokenResult = OAuthApi.GetAccessToken(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
- oAuthAccessTokenBag.OAuthAccessTokenExpireTime =
- ApiUtility.GetExpireTime(oAuthAccessTokenBag.OAuthAccessTokenResult.expires_in);
- Update(oAuthAccessTokenBag, null);
- }
- }
- return oAuthAccessTokenBag.OAuthAccessTokenResult;
- }
- #endregion
- #endregion
- #if !NET35 && !NET40
- #region 异步方法
- #region OAuthAccessToken
- /// <summary>
- /// 【异步方法】使用完整的应用凭证获取Ticket,如果不存在将自动注册
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="appSecret"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken"></param>
- /// <returns></returns>
- public static async Task<string> TryGetOAuthAccessTokenAsync(string appId, string appSecret, string code, bool getNewToken = false)
- {
- if (!CheckRegistered(appId) || getNewToken)
- {
- Register(appId, appSecret);
- }
- return await GetOAuthAccessTokenAsync(appId, code, getNewToken);
- }
- /// <summary>
- /// 【异步方法】获取可用Ticket
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
- /// <returns></returns>
- public static async Task<string> GetOAuthAccessTokenAsync(string appId, string code, bool getNewToken = false)
- {
- var result = await GetOAuthAccessTokenResultAsync(appId, code, getNewToken);
- return result.access_token;
- }
- /// <summary>
- /// 【异步方法】获取可用Ticket
- /// </summary>
- /// <param name="appId"></param>
- /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
- /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
- /// <returns></returns>
- public static async Task<OAuthAccessTokenResult> GetOAuthAccessTokenResultAsync(string appId, string code, bool getNewToken = false)
- {
- if (!CheckRegistered(appId))
- {
- throw new UnRegisterAppIdException(null, "此appId尚未注册,请先使用OAuthAccessTokenContainer.Register完成注册(全局执行一次即可)!");
- }
- var oAuthAccessTokenBag = TryGetItem(appId);
- using (Cache.BeginCacheLock(LockResourceName, appId))//同步锁
- {
- if (getNewToken || oAuthAccessTokenBag.OAuthAccessTokenExpireTime <= SystemTime.Now)
- {
- //已过期,重新获取
- var oAuthAccessTokenResult = await OAuthApi.GetAccessTokenAsync(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
- oAuthAccessTokenBag.OAuthAccessTokenResult = oAuthAccessTokenResult;
- //oAuthAccessTokenBag.OAuthAccessTokenResult = OAuthApi.GetAccessToken(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
- oAuthAccessTokenBag.OAuthAccessTokenExpireTime =
- ApiUtility.GetExpireTime(oAuthAccessTokenBag.OAuthAccessTokenResult.expires_in);
- Update(oAuthAccessTokenBag, null);
- }
- }
- return oAuthAccessTokenBag.OAuthAccessTokenResult;
- }
- #endregion
- #endregion
- #endif
- }
- }
|