senparc 第三方授权回调,保存授权信息到文件
[HttpGet] public async Task<IActionResult> OpenOAuthCallback(string auth_code, string appletId) { try { #region 使用ComponentContainer //获取OAuth授权结果 QueryAuthResult queryAuthResult; try { queryAuthResult = ComponentContainer.GetQueryAuthResult(_senparcWeixinSetting.Component_Appid, auth_code, false); } catch (Exception ex) { throw new Exception("QueryAuthResult:" + ex.Message); } #endregion if (queryAuthResult.authorization_info == null) return Json(queryAuthResult); var dir = Path.Combine(Environment.CurrentDirectory + @"/App_Data/AuthorizerInfo/" + _senparcWeixinSetting.Component_Appid); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var file = Path.Combine(dir, string.Format("{0}.bin", queryAuthResult.authorization_info.authorizer_appid)); //记录authorization_info(也可以存入数据库或其他可以持久化的地方) using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(fs, new RefreshAuthorizerTokenResult { authorizer_access_token = queryAuthResult.authorization_info.authorizer_access_token, expires_in = queryAuthResult.authorization_info.expires_in, authorizer_refresh_token = queryAuthResult.authorization_info.authorizer_refresh_token }); } //授权成功后,更新客户小程序信息 queryAuthResult.authorization_info.miniprograminf var getAuthorizerInfoResult = AuthorizerContainer.GetAuthorizerInfoResult(_senparcWeixinSetting.Component_Appid, queryAuthResult.authorization_info.authorizer_appid); return Json(queryAuthResult.authorization_info); } catch (ErrorJsonResultException ex) { return Content(ex.Message); } }