Skip to content

Commit

Permalink
Merge pull request #598 from JeffreySu/Developer
Browse files Browse the repository at this point in the history
Open v2.6.0 为GetAuthorizerRefreshTokenFunc及AuthorizerTokenRefreshedFu…
  • Loading branch information
JeffreySu authored Jun 15, 2017
2 parents c71f952 + 27f7e52 commit 884637e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,46 +198,46 @@ private void RegisterWeixinThirdParty()
}
};

Func<string, string> 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<string, RefreshAuthorizerTokenResult> authorizerTokenRefreshedFunc = (auhtorizerId, refreshResult) =>
Func<string, string, string> 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<string, string, RefreshAuthorizerTokenResult> 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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -477,7 +477,7 @@ public static JsApiTicketResult GetJsApiTicketResult(string componentAppId, stri
}

#endregion

#endregion

#region 异步方法
Expand Down Expand Up @@ -507,14 +507,14 @@ public static async Task<AuthorizationInfo> 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);

//更新数据
Expand Down Expand Up @@ -601,7 +601,7 @@ public static async Task<RefreshAuthorizerTokenResult> RefreshAuthorizerTokenAsy
var refreshResult = await ComponentApi.ApiAuthorizerTokenAsync(componentAccessToken, componentAppId, authorizerAppid,
refreshToken);
//更新到存储
ComponentContainer.AuthorizerTokenRefreshedFunc(authorizerAppid, refreshResult);
ComponentContainer.AuthorizerTokenRefreshedFunc(componentAppId, authorizerAppid, refreshResult);
return refreshResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ private static void TryRegister(string componentAppId, string componentAppSecret
/// <summary>
/// 从数据库中获取已存的AuthorizerAccessToken的方法
/// </summary>
public static Func<string, string> GetAuthorizerRefreshTokenFunc { get; set; }
public static Func<string, string, string> GetAuthorizerRefreshTokenFunc { get; set; }

/// <summary>
/// AuthorizerAccessToken更新后的回调
/// </summary>
public static Action<string, RefreshAuthorizerTokenResult> AuthorizerTokenRefreshedFunc = null;
public static Action<string, string, RefreshAuthorizerTokenResult> AuthorizerTokenRefreshedFunc = null;


/// <summary>
Expand All @@ -236,7 +236,7 @@ private static void TryRegister(string componentAppId, string componentAppSecret
/// <param name="getAuthorizerRefreshTokenFunc">从数据库中获取已存的AuthorizerAccessToken的方法</param>
/// <param name="authorizerTokenRefreshedFunc">AuthorizerAccessToken更新后的回调</param>
/// <param name="name">标记Authorizer名称(如微信公众号名称),帮助管理员识别</param>
public static void Register(string componentAppId, string componentAppSecret, Func<string, string> getComponentVerifyTicketFunc, Func<string, string> getAuthorizerRefreshTokenFunc, Action<string, RefreshAuthorizerTokenResult> authorizerTokenRefreshedFunc, string name = null)
public static void Register(string componentAppId, string componentAppSecret, Func<string, string> getComponentVerifyTicketFunc, Func<string, string, string> getAuthorizerRefreshTokenFunc, Action<string, string, RefreshAuthorizerTokenResult> authorizerTokenRefreshedFunc, string name = null)
{
//激活消息队列线程

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.5.0.*")]
[assembly: AssemblyVersion("2.6.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 884637e

Please sign in to comment.