From 27f7e5217f9a3b5232ba4b7b77ead5178ccaa760 Mon Sep 17 00:00:00 2001 From: Jeffrey Su Date: Fri, 16 Jun 2017 00:28:08 +0800 Subject: [PATCH] =?UTF-8?q?Open=20v2.6.0=20=E4=B8=BAGetAuthorizerRefreshTo?= =?UTF-8?q?kenFunc=E5=8F=8AAuthorizerTokenRefreshedFunc=E6=B7=BB=E5=8A=A0c?= =?UTF-8?q?omponentAppId=E5=8F=82=E6=95=B0=EF=BC=8C=E7=94=A8=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=9A=E5=9C=A8=E5=90=8C=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=B8=AD=E5=BC=80=E8=AE=BE=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E5=BC=80=E6=94=BE=E5=B9=B3=E5=8F=B0=EF=BC=8C=E5=B9=B6=E5=B7=AE?= =?UTF-8?q?=E5=88=AB=E5=8C=96=E5=AF=B9=E5=BE=85=E5=90=8C=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/JeffreySu/WeiXinMPSDK/issues/543 感谢 @ytpos --- .../Senparc.Weixin.MP.Sample/Global.asax.cs | 56 +++++++++---------- .../Containers/AuthorizerContainer.cs | 16 +++--- .../Containers/ComponentContainer.cs | 6 +- .../Properties/AssemblyInfo.cs | 2 +- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Global.asax.cs b/src/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Global.asax.cs index ab746e37b6..57ac9a8f80 100644 --- a/src/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Global.asax.cs +++ b/src/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Global.asax.cs @@ -198,46 +198,46 @@ private void RegisterWeixinThirdParty() } }; - Func getAuthorizerRefreshTokenFunc = auhtorizerId => - { - var dir = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\AuthorizerInfo"); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - - var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); - if (!File.Exists(file)) - { - return null; - } - - using (Stream fs = new FileStream(file, FileMode.Open)) - { - BinaryFormatter binFormat = new BinaryFormatter(); - var result = (RefreshAuthorizerTokenResult)binFormat.Deserialize(fs); - return result.authorizer_refresh_token; - } - }; - - Action authorizerTokenRefreshedFunc = (auhtorizerId, refreshResult) => + Func getAuthorizerRefreshTokenFunc = (componentAppId, auhtorizerId) => { - var dir = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\AuthorizerInfo"); + var dir = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\AuthorizerInfo\\" + componentAppId); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); - using (Stream fs = new FileStream(file, FileMode.Create)) + if (!File.Exists(file)) + { + return null; + } + + using (Stream fs = new FileStream(file, FileMode.Open)) { - //这里存了整个对象,实际上只存RefreshToken也可以,有了RefreshToken就能刷新到最新的AccessToken BinaryFormatter binFormat = new BinaryFormatter(); - binFormat.Serialize(fs, refreshResult); - fs.Flush(); + var result = (RefreshAuthorizerTokenResult)binFormat.Deserialize(fs); + return result.authorizer_refresh_token; } }; + Action authorizerTokenRefreshedFunc = (componentAppId, auhtorizerId, refreshResult) => + { + var dir = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data\\AuthorizerInfo\\" + componentAppId); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); + using (Stream fs = new FileStream(file, FileMode.Create)) + { + //这里存了整个对象,实际上只存RefreshToken也可以,有了RefreshToken就能刷新到最新的AccessToken + BinaryFormatter binFormat = new BinaryFormatter(); + binFormat.Serialize(fs, refreshResult); + fs.Flush(); + } + }; + //执行注册 ComponentContainer.Register( ConfigurationManager.AppSettings["Component_Appid"], diff --git a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/AuthorizerContainer.cs b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/AuthorizerContainer.cs index 192b38e72c..54be17a78b 100644 --- a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/AuthorizerContainer.cs +++ b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/AuthorizerContainer.cs @@ -254,7 +254,7 @@ public static AuthorizationInfo GetAuthorizationInfo(string componentAppId, stri var componentAccessToken = ComponentContainer.GetComponentAccessToken(componentAppId, componentVerifyTicket); //获取新的AuthorizerAccessToken - var refreshToken = ComponentContainer.GetAuthorizerRefreshTokenFunc(authorizerAppid); + var refreshToken = ComponentContainer.GetAuthorizerRefreshTokenFunc(componentAppId, authorizerAppid); if (refreshToken == null) { @@ -358,7 +358,7 @@ public static void TryUpdateAuthorizationInfo(string componentAppId, string auth //通知变更 if (refreshTokenChanged) { - ComponentContainer.AuthorizerTokenRefreshedFunc(authorizerAppid, + ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, new RefreshAuthorizerTokenResult(authorizationInfo.authorizer_access_token, authorizationInfo.authorizer_refresh_token, authorizationInfo.expires_in)); } @@ -394,7 +394,7 @@ public static void TryUpdateAuthorizationInfo(string componentAppId, string auth //通知变更 if (refreshTokenChanged) { - ComponentContainer.AuthorizerTokenRefreshedFunc(authorizerAppid, + ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, new RefreshAuthorizerTokenResult(authorizerAccessToken, authorizerRefreshToken, expiresIn)); } } @@ -414,7 +414,7 @@ public static RefreshAuthorizerTokenResult RefreshAuthorizerToken(string compone var refreshResult = ComponentApi.ApiAuthorizerToken(componentAccessToken, componentAppId, authorizerAppid, refreshToken); //更新到存储 - ComponentContainer.AuthorizerTokenRefreshedFunc(authorizerAppid, refreshResult); + ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, refreshResult); return refreshResult; } @@ -477,7 +477,7 @@ public static JsApiTicketResult GetJsApiTicketResult(string componentAppId, stri } #endregion - + #endregion #region 异步方法 @@ -507,14 +507,14 @@ public static async Task GetAuthorizationInfoAsync(string com var componentAccessToken = await ComponentContainer.GetComponentAccessTokenAsync(componentAppId, componentVerifyTicket); //获取新的AuthorizerAccessToken - var refreshToken = ComponentContainer.GetAuthorizerRefreshTokenFunc(authorizerAppid); + var refreshToken = ComponentContainer.GetAuthorizerRefreshTokenFunc(componentAppId, authorizerAppid); if (refreshToken == null) { return null; } - var refreshResult =await RefreshAuthorizerTokenAsync(componentAccessToken, componentAppId, authorizerAppid, + var refreshResult = await RefreshAuthorizerTokenAsync(componentAccessToken, componentAppId, authorizerAppid, refreshToken); //更新数据 @@ -601,7 +601,7 @@ public static async Task RefreshAuthorizerTokenAsy var refreshResult = await ComponentApi.ApiAuthorizerTokenAsync(componentAccessToken, componentAppId, authorizerAppid, refreshToken); //更新到存储 - ComponentContainer.AuthorizerTokenRefreshedFunc(authorizerAppid, refreshResult); + ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, refreshResult); return refreshResult; } diff --git a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/ComponentContainer.cs b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/ComponentContainer.cs index f8e62290a7..62e8657491 100644 --- a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/ComponentContainer.cs +++ b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Containers/ComponentContainer.cs @@ -219,12 +219,12 @@ private static void TryRegister(string componentAppId, string componentAppSecret /// /// 从数据库中获取已存的AuthorizerAccessToken的方法 /// - public static Func GetAuthorizerRefreshTokenFunc { get; set; } + public static Func GetAuthorizerRefreshTokenFunc { get; set; } /// /// AuthorizerAccessToken更新后的回调 /// - public static Action AuthorizerTokenRefreshedFunc = null; + public static Action AuthorizerTokenRefreshedFunc = null; /// @@ -236,7 +236,7 @@ private static void TryRegister(string componentAppId, string componentAppSecret /// 从数据库中获取已存的AuthorizerAccessToken的方法 /// AuthorizerAccessToken更新后的回调 /// 标记Authorizer名称(如微信公众号名称),帮助管理员识别 - public static void Register(string componentAppId, string componentAppSecret, Func getComponentVerifyTicketFunc, Func getAuthorizerRefreshTokenFunc, Action authorizerTokenRefreshedFunc, string name = null) + public static void Register(string componentAppId, string componentAppSecret, Func getComponentVerifyTicketFunc, Func getAuthorizerRefreshTokenFunc, Action authorizerTokenRefreshedFunc, string name = null) { //激活消息队列线程 diff --git a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Properties/AssemblyInfo.cs b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Properties/AssemblyInfo.cs index 6013eedfbd..bf7b9408c8 100644 --- a/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Properties/AssemblyInfo.cs +++ b/src/Senparc.Weixin.Open/Senparc.Weixin.Open/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.5.0.*")] +[assembly: AssemblyVersion("2.6.0.*")] //[assembly: AssemblyFileVersion("1.0.0.0")]