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 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)); }