/// <summary> /// 获取跳转到授权页链接 /// </summary> /// <returns></returns> public ActionResult getOpenImg() { var msg = string.Empty; try { var model = opencomponentconfigBLL.SingleModel.getCurrentModel(); if (model != null) { var pre_token = ComponentApi.GetPreAuthCode(model.component_Appid, model.component_access_token); if (pre_token != null) { var returnurl = "http://chx.vzan.com/XcxManage/Index?cityInfoId=695"; var callbackurl = "http://openapp.vzan.com/Test/OpenOAuthCallback?AreaCode=110228&returnurl=" + returnurl; var url = XcxApis.XcxApis_tiaozhuan(model.component_Appid, pre_token.pre_auth_code, callbackurl); msg = url; } } //return Redirect(msg); return(Json(new { code = 1, src = msg, msg = "" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { code = 0, src = "", msg = ex.Message + "," + msg }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 获取可用Token /// </summary> /// <param name="componentAppId"></param> /// <param name="getNewToken">是否强制重新获取新的Token</param> /// <returns></returns> public static PreAuthCodeResult GetPreAuthCodeResult(string componentAppId, bool getNewToken = false) { if (!CheckRegistered(componentAppId)) { throw new WeixinOpenException(UN_REGISTER_ALERT); } var componentBag = TryGetItem(componentAppId); using (Cache.BeginCacheLock(LockResourceName + ".GetPreAuthCodeResult", componentAppId))//同步锁 { if (getNewToken || componentBag.PreAuthCodeExpireTime <= DateTime.Now) { //已过期,重新获取 var componentVerifyTicket = TryGetComponentVerifyTicket(componentAppId); var accessToken = TryGetComponentAccessToken(componentAppId, componentBag.ComponentAppSecret, componentVerifyTicket); var preAuthCodeResult = ComponentApi.GetPreAuthCode(componentBag.ComponentAppId, accessToken); componentBag.PreAuthCodeExpireTime = ApiUtility.GetExpireTime(preAuthCodeResult.expires_in); componentBag.PreAuthCodeResult = preAuthCodeResult; ////TODO:这里有出现expires_in=0的情况,导致始终处于过期状态(也可能是因为参数过期等原因没有返回正确的数据,待观察) //var expiresIn = componentBag.PreAuthCodeResult.expires_in > 0 // ? componentBag.PreAuthCodeResult.expires_in // : 60 * 20;//默认为20分钟 //componentBag.PreAuthCodeExpireTime = DateTime.Now.AddSeconds(expiresIn); } } return(componentBag.PreAuthCodeResult); }
public ActionResult GotoOAuth(int shopId) { if (shopId <= 0) { throw new ArgumentNullException("shopId"); } CheckShopActor(shopId, ShopActorType.超级管理员); //确认当前shopId是否已经绑定了小程序 var model = db.Query <ShopWechatOpenAuthorizer>() .Where(m => !m.IsDel) .Where(m => m.ShopId == shopId) .FirstOrDefault(); if (model != null) { throw new Exception("当前店铺绑定的小程序已经存在"); } //获取预授权码 //var preAuthCode = ComponentContainer.TryGetPreAuthCode(wechatOpenOptions.AppId, wechatOpenOptions.AppSecret, true);\ string componentAccessToken = ZRui.Web.BLL.AuthorizerHelper.GetComponentAccessToken(); var log = BLL.DbContextFactory.LogDbContext; var preAuthCode = ComponentApi.GetPreAuthCode(wechatOpenOptions.AppId, componentAccessToken, 0x2710).pre_auth_code; var callbackUrl = $"http://manager.91huichihuihe.com/ShopWechatOpenOAuth/OAuthCallback?shopId={shopId}";//成功回调地址 var url = ComponentApi.GetComponentLoginPageUrl(wechatOpenOptions.AppId, preAuthCode, callbackUrl); return(Redirect(url)); }
public override Task <GetBindUrlResponse> GetBindUrl(GetBindUrlRequest request, ServerCallContext context) { var resp = new GetBindUrlResponse(); //获取preauthcode var preCode = ComponentApi.GetPreAuthCode(_componentAccessToken, _config["Wechat:AppID"]); if (preCode.ErrCode == 0) { if (request.UseMobile) { resp.Url = ComponentApi.GetBindUrl(_componentAppId, preCode.PreAuthCode, request.RedirectUrl, true); } else { resp.Url = ComponentApi.GetBindUrl(_componentAppId, preCode.PreAuthCode, request.RedirectUrl); } } else { resp.Error = new Error { ErrCode = preCode.ErrCode, ErrMsg = preCode.ErrMsg }; } return(Task.FromResult(resp)); }
public override string OnComponentVerifyTicketRequest(RequestMessageComponentVerifyTicket requestMessage) { ComponentTokenService cts = new ComponentTokenService(); var componentToken = cts.GetToken(); componentToken.ComponentVerifyTicketCreateOn = DateTime.Now; componentToken.ComponentVerifyTicket = requestMessage.ComponentVerifyTicket; cts.SaveVerifyToken(componentToken); var expiredTime = componentToken.ComponentAccessTokenCreateOn.AddSeconds(componentToken.ComponentAccessTokenExpiresIn); if (ExpiresIn(expiredTime, 1200)) { //Refresh the token before 1200 seconds when it expired try { var updatedToken = ComponentApi.GetComponentAccessToken(ConfigurationManager.AppSettings["AppId"], ConfigurationManager.AppSettings["AppSecret"], componentToken.ComponentVerifyTicket); componentToken.ComponentAccessTokenCreateOn = DateTime.Now; componentToken.ComponentAccessTokenExpiresIn = updatedToken.expires_in; componentToken.ComponentAccessToken = updatedToken.component_access_token; cts.SaveAccessToken(componentToken); Log("update access token to " + JsonConvert.SerializeObject(componentToken)); } catch (Exception e) { Log(e.ToString(), true); } } expiredTime = componentToken.PreAuthCodeCreateOn.AddSeconds(componentToken.PreAuthCodeExpiresIn); if (ExpiresIn(expiredTime, 1200)) { try { var updatedCode = ComponentApi.GetPreAuthCode(ConfigurationManager.AppSettings["AppId"], componentToken.ComponentAccessToken); componentToken.PreAuthCodeExpiresIn = updatedCode.expires_in; componentToken.PreAuthCode = updatedCode.pre_auth_code; componentToken.PreAuthCodeCreateOn = DateTime.Now; cts.SavePreAuthCode(componentToken); Log("update preauth to " + JsonConvert.SerializeObject(componentToken)); } catch (Exception e2) { Log(e2.ToString(), true); } } return(base.OnComponentVerifyTicketRequest(requestMessage)); }
public void UpdatePreAuthCode() { var pac = ComponentKeys.GetInstance().PreAuthData; var pacRlt = ComponentApi.GetPreAuthCode(_wxConfig.AppId, ComponentKeys.GetInstance().AccessData.AccessCode); pac.PreAuthCode = pacRlt.pre_auth_code; pac.ExpiresIn = pacRlt.expires_in; pac.RefreshOn = DateTime.Now; //store to db basicToken.PreAuthCode = pac.PreAuthCode; basicToken.PreAuthCodeRefreshOn = pac.RefreshOn; basicToken.PreAuthCodeExpiresIn = pac.ExpiresIn; }
public ActionResult Installed(string auth_code, int expires_in) { var cts = new ComponentTokenService(); var componentToken = cts.GetToken(); string componentAppId = ConfigurationManager.AppSettings["AppId"]; var queryAuth = Senparc.Weixin.Open.ComponentAPIs.ComponentApi.QueryAuth( componentToken.ComponentAccessToken, componentAppId, auth_code); string authorizerAppid = queryAuth.authorization_info.authorizer_appid; var authorizerInfoResult = ComponentApi.GetAuthorizerInfo(componentToken.ComponentAccessToken, componentAppId, queryAuth.authorization_info.authorizer_appid); var authorizerInfo = authorizerInfoResult.authorizer_info; var authorizerInfoEntity = db.MpInfos.FirstOrDefault(c => c.UserName == authorizerInfo.user_name); if (authorizerInfoEntity == null) { authorizerInfoEntity = new MpInfo() { UserName = authorizerInfo.user_name, NickName = authorizerInfo.nick_name, HeadImg = authorizerInfo.head_img, ServiceType = (int)authorizerInfo.service_type_info.id, VerifyType = (int)authorizerInfo.verify_type_info.id, PrincipalName = authorizerInfo.principal_name, BizStore = authorizerInfo.business_info.open_store, BizPay = authorizerInfo.business_info.open_pay, BizCard = authorizerInfo.business_info.open_card, BizScan = authorizerInfo.business_info.open_scan, BizShake = authorizerInfo.business_info.open_shake, Alias = authorizerInfo.alias, QrcodeUrl = authorizerInfo.qrcode_url }; db.MpInfos.Add(authorizerInfoEntity); } MpToken token = db.MpTokens.FirstOrDefault(c => c.MpAppId == authorizerAppid); if (token == null) { token = new MpToken(); token.MpAppId = authorizerAppid; db.MpTokens.Add(token); } token.RefreshOn = DateTime.Now; token.MpAccessToken = queryAuth.authorization_info.authorizer_access_token; token.MpRefreshToken = queryAuth.authorization_info.authorizer_refresh_token; token.ExpiredIn = queryAuth.authorization_info.expires_in; token.BelongToMp = authorizerInfoEntity; db.SaveChanges(); //update preauthcode var updatedCode = ComponentApi.GetPreAuthCode(ConfigurationManager.AppSettings["AppId"], componentToken.ComponentAccessToken); componentToken.PreAuthCodeExpiresIn = updatedCode.expires_in; componentToken.PreAuthCode = updatedCode.pre_auth_code; componentToken.PreAuthCodeCreateOn = DateTime.Now; cts.SavePreAuthCode(componentToken); //HomeInstalledViewModel vm = new HomeInstalledViewModel(); //vm.AuthorizerAppId = authorizerAppid; //vm.AuthUrl = string.Format(ConfigurationManager.AppSettings["UserAuthEntryPointUriFmt"], authorizerAppid); string redirectUrl = string.Format(ConfigurationManager.AppSettings["InstallSuccessUrl"], authorizerAppid); return(Redirect(redirectUrl)); }