OAuthAccessTokenContainer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:OAuthContainer.cs
  17. 文件功能描述:用户OAuth容器,用于自动管理OAuth的AccessToken,如果过期会重新获取
  18. 创建标识:Senparc - 20160801
  19. 修改标识:Senparc - 20160803
  20. 修改描述:v14.2.3 使用ApiUtility.GetExpireTime()方法处理过期
  21. 修改标识:Senparc - 20160804
  22. 修改描述:v14.2.4 增加TryGetOAuthAccessTokenAsync,GetOAuthAccessTokenAsync,GetOAuthAccessTokenResultAsync的异步方法
  23. 修改标识:Senparc - 20160808
  24. 修改描述:v14.3.0 删除 ItemCollection 属性,直接使用ContainerBag加入到缓存
  25. 修改标识:Senparc - 20160813
  26. 修改描述:v14.3.4 添加TryReRegister()方法,处理分布式缓存重启(丢失)的情况
  27. 修改标识:Senparc - 20160813
  28. 修改描述:v14.3.6 完善getNewToken参数传递
  29. 修改标识:Senparc - 20180614
  30. 修改描述:CO2NET v0.1.0 ContainerBag 取消属性变动通知机制,使用手动更新缓存
  31. 修改标识:Senparc - 20180707
  32. 修改描述:v15.0.9 Container 的 Register() 的微信参数自动添加到 Config.SenparcWeixinSetting.Items 下
  33. 修改标识:Senparc - 20181226
  34. 修改描述:v16.6.2 修改 DateTime 为 DateTimeOffset
  35. ----------------------------------------------------------------*/
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Threading.Tasks;
  40. using Senparc.Weixin.Cache;
  41. using Senparc.Weixin.Containers;
  42. using Senparc.Weixin.Exceptions;
  43. using Senparc.Weixin.MP.Entities;
  44. using Senparc.CO2NET.CacheUtility;
  45. using Senparc.Weixin.MP.AdvancedAPIs;
  46. using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
  47. using Senparc.Weixin.MP.CommonAPIs;
  48. using Senparc.Weixin.Utilities.WeixinUtility;
  49. using Senparc.CO2NET.Extensions;
  50. namespace Senparc.Weixin.MP.Containers
  51. {
  52. /// <summary>
  53. /// OAuth包
  54. /// </summary>
  55. [Serializable]
  56. public class OAuthAccessTokenBag : BaseContainerBag, IBaseContainerBag_AppId
  57. {
  58. public string AppId { get; set; }
  59. // {
  60. // get { return _appId; }
  61. //#if NET35 || NET40
  62. // set { this.SetContainerProperty(ref _appId, value, "AppId"); }
  63. //#else
  64. // set { this.SetContainerProperty(ref _appId, value); }
  65. //#endif
  66. // }
  67. public string AppSecret { get; set; }
  68. // {
  69. // get { return _appSecret; }
  70. //#if NET35 || NET40
  71. // set { this.SetContainerProperty(ref _appSecret, value, "AppSecret"); }
  72. //#else
  73. // set { this.SetContainerProperty(ref _appSecret, value); }
  74. //#endif
  75. // }
  76. public OAuthAccessTokenResult OAuthAccessTokenResult { get; set; }
  77. // {
  78. // get { return _oAuthAccessTokenResult; }
  79. //#if NET35 || NET40
  80. // set { this.SetContainerProperty(ref _oAuthAccessTokenResult, value, "OAuthAccessTokenResult"); }
  81. //#else
  82. // set { this.SetContainerProperty(ref _oAuthAccessTokenResult, value); }
  83. //#endif
  84. // }
  85. public DateTimeOffset OAuthAccessTokenExpireTime { get; set; }
  86. // {
  87. // get { return _oAuthAccessTokenExpireTime; }
  88. //#if NET35 || NET40
  89. // set { this.SetContainerProperty(ref _oAuthAccessTokenExpireTime, value, "OAuthAccessTokenExpireTime"); }
  90. //#else
  91. // set { this.SetContainerProperty(ref _oAuthAccessTokenExpireTime, value); }
  92. //#endif
  93. // }
  94. /// <summary>
  95. /// 只针对这个AppId的锁
  96. /// </summary>
  97. internal object Lock = new object();
  98. //private DateTimeOffset _oAuthAccessTokenExpireTime;
  99. //private OAuthAccessTokenResult _oAuthAccessTokenResult;
  100. //private string _appSecret;
  101. //private string _appId;
  102. }
  103. /// <summary>
  104. /// 用户OAuth容器,用于自动管理OAuth的AccessToken,如果过期会重新获取(测试中,暂时别用)
  105. /// </summary>
  106. public class OAuthAccessTokenContainer : BaseContainer<OAuthAccessTokenBag>
  107. {
  108. const string LockResourceName = "MP.OAuthAccessTokenContainer";
  109. #region 同步方法
  110. //static Dictionary<string, JsApiTicketBag> JsApiTicketCollection =
  111. // new Dictionary<string, JsApiTicketBag>(StringComparer.OrdinalIgnoreCase);
  112. /// <summary>
  113. /// 注册应用凭证信息,此操作只是注册,不会马上获取Ticket,并将清空之前的Ticket,
  114. /// </summary>
  115. /// <param name="appId"></param>
  116. /// <param name="appSecret"></param>
  117. /// <param name="name">标记JsApiTicket名称(如微信公众号名称),帮助管理员识别。当 name 不为 null 和 空值时,本次注册内容将会被记录到 Senparc.Weixin.Config.SenparcWeixinSetting.Items[name] 中,方便取用。</param>
  118. /// 此接口不提供异步方法
  119. public static void Register(string appId, string appSecret, string name = null)
  120. {
  121. RegisterFunc = () =>
  122. {
  123. //using (FlushCache.CreateInstance())
  124. //{
  125. var bag = new OAuthAccessTokenBag()
  126. {
  127. Name = name,
  128. AppId = appId,
  129. AppSecret = appSecret,
  130. OAuthAccessTokenExpireTime = DateTimeOffset.MinValue,
  131. OAuthAccessTokenResult = new OAuthAccessTokenResult()
  132. };
  133. Update(appId, bag, null);
  134. return bag;
  135. //}
  136. };
  137. RegisterFunc();
  138. if (!name.IsNullOrEmpty())
  139. {
  140. Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinAppId = appId;
  141. Senparc.Weixin.Config.SenparcWeixinSetting.Items[name].WeixinAppSecret = appSecret;
  142. }
  143. }
  144. #region OAuthAccessToken
  145. /// <summary>
  146. /// 使用完整的应用凭证获取Ticket,如果不存在将自动注册
  147. /// </summary>
  148. /// <param name="appId"></param>
  149. /// <param name="appSecret"></param>
  150. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  151. /// <param name="getNewToken"></param>
  152. /// <returns></returns>
  153. public static string TryGetOAuthAccessToken(string appId, string appSecret, string code, bool getNewToken = false)
  154. {
  155. if (!CheckRegistered(appId) || getNewToken)
  156. {
  157. Register(appId, appSecret);
  158. }
  159. return GetOAuthAccessToken(appId, code, getNewToken);
  160. }
  161. /// <summary>
  162. /// 获取可用Ticket
  163. /// </summary>
  164. /// <param name="appId"></param>
  165. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  166. /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
  167. /// <returns></returns>
  168. public static string GetOAuthAccessToken(string appId, string code, bool getNewToken = false)
  169. {
  170. return GetOAuthAccessTokenResult(appId, code, getNewToken).access_token;
  171. }
  172. /// <summary>
  173. /// 获取可用Ticket
  174. /// </summary>
  175. /// <param name="appId"></param>
  176. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  177. /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
  178. /// <returns></returns>
  179. public static OAuthAccessTokenResult GetOAuthAccessTokenResult(string appId, string code, bool getNewToken = false)
  180. {
  181. if (!CheckRegistered(appId))
  182. {
  183. throw new UnRegisterAppIdException(null, "此appId尚未注册,请先使用OAuthAccessTokenContainer.Register完成注册(全局执行一次即可)!");
  184. }
  185. var oAuthAccessTokenBag = TryGetItem(appId);
  186. using (Cache.BeginCacheLock(LockResourceName, appId))//同步锁
  187. {
  188. if (getNewToken || oAuthAccessTokenBag.OAuthAccessTokenExpireTime <= SystemTime.Now)
  189. {
  190. //已过期,重新获取
  191. oAuthAccessTokenBag.OAuthAccessTokenResult = OAuthApi.GetAccessToken(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
  192. oAuthAccessTokenBag.OAuthAccessTokenExpireTime =
  193. ApiUtility.GetExpireTime(oAuthAccessTokenBag.OAuthAccessTokenResult.expires_in);
  194. Update(oAuthAccessTokenBag, null);
  195. }
  196. }
  197. return oAuthAccessTokenBag.OAuthAccessTokenResult;
  198. }
  199. #endregion
  200. #endregion
  201. #if !NET35 && !NET40
  202. #region 异步方法
  203. #region OAuthAccessToken
  204. /// <summary>
  205. /// 【异步方法】使用完整的应用凭证获取Ticket,如果不存在将自动注册
  206. /// </summary>
  207. /// <param name="appId"></param>
  208. /// <param name="appSecret"></param>
  209. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  210. /// <param name="getNewToken"></param>
  211. /// <returns></returns>
  212. public static async Task<string> TryGetOAuthAccessTokenAsync(string appId, string appSecret, string code, bool getNewToken = false)
  213. {
  214. if (!CheckRegistered(appId) || getNewToken)
  215. {
  216. Register(appId, appSecret);
  217. }
  218. return await GetOAuthAccessTokenAsync(appId, code, getNewToken);
  219. }
  220. /// <summary>
  221. /// 【异步方法】获取可用Ticket
  222. /// </summary>
  223. /// <param name="appId"></param>
  224. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  225. /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
  226. /// <returns></returns>
  227. public static async Task<string> GetOAuthAccessTokenAsync(string appId, string code, bool getNewToken = false)
  228. {
  229. var result = await GetOAuthAccessTokenResultAsync(appId, code, getNewToken);
  230. return result.access_token;
  231. }
  232. /// <summary>
  233. /// 【异步方法】获取可用Ticket
  234. /// </summary>
  235. /// <param name="appId"></param>
  236. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  237. /// <param name="getNewToken">是否强制重新获取新的Ticket</param>
  238. /// <returns></returns>
  239. public static async Task<OAuthAccessTokenResult> GetOAuthAccessTokenResultAsync(string appId, string code, bool getNewToken = false)
  240. {
  241. if (!CheckRegistered(appId))
  242. {
  243. throw new UnRegisterAppIdException(null, "此appId尚未注册,请先使用OAuthAccessTokenContainer.Register完成注册(全局执行一次即可)!");
  244. }
  245. var oAuthAccessTokenBag = TryGetItem(appId);
  246. using (Cache.BeginCacheLock(LockResourceName, appId))//同步锁
  247. {
  248. if (getNewToken || oAuthAccessTokenBag.OAuthAccessTokenExpireTime <= SystemTime.Now)
  249. {
  250. //已过期,重新获取
  251. var oAuthAccessTokenResult = await OAuthApi.GetAccessTokenAsync(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
  252. oAuthAccessTokenBag.OAuthAccessTokenResult = oAuthAccessTokenResult;
  253. //oAuthAccessTokenBag.OAuthAccessTokenResult = OAuthApi.GetAccessToken(oAuthAccessTokenBag.AppId, oAuthAccessTokenBag.AppSecret, code);
  254. oAuthAccessTokenBag.OAuthAccessTokenExpireTime =
  255. ApiUtility.GetExpireTime(oAuthAccessTokenBag.OAuthAccessTokenResult.expires_in);
  256. Update(oAuthAccessTokenBag, null);
  257. }
  258. }
  259. return oAuthAccessTokenBag.OAuthAccessTokenResult;
  260. }
  261. #endregion
  262. #endregion
  263. #endif
  264. }
  265. }