Beispiel #1
0
        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));
        }
Beispiel #2
0
        public ActionResult Install()
        {
            //授权成功后返回的 /Home/Installed?auth_code=queryauthcode@@@tKlkuvs2i5XuP3wloLDuauVHnQ4kZdU6LPczHEAarkABxUURgl9hOy_YHb_Ndsn8uu6j6Uv1za9q1ecmHi4MvQ&expires_in=3600
            var cts = new ComponentTokenService();
            HomeInstallViewModel vm = new HomeInstallViewModel();

            vm.WxAppId     = ConfigurationManager.AppSettings["AppId"];
            vm.RedirectUri = ConfigurationManager.AppSettings["RedirectUri"];
            vm.PreAuthCode = cts.GetToken().PreAuthCode;
            return(View(vm));
        }
Beispiel #3
0
        public IHttpActionResult RetrieveInstallUrl()
        {
            var cts         = new ComponentTokenService();
            var WxAppId     = ConfigurationManager.AppSettings["AppId"];
            var RedirectUri = ConfigurationManager.AppSettings["RedirectUri"];
            var PreAuthCode = cts.GetToken().PreAuthCode;

            string urlFormat  = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={0}&pre_auth_code={1}&redirect_uri={2}";
            string installUrl = string.Format(urlFormat, WxAppId, PreAuthCode, RedirectUri);

            var result = new { installUrl = installUrl };

            return(Ok(result));
        }
Beispiel #4
0
        private void RefreshMpAccessCode()
        {
            var    cts            = new ComponentTokenService();
            var    componentToken = cts.GetToken();
            string componentAppId = ConfigurationManager.AppSettings["AppId"];
            var    mpTokenList    = _context.MpTokens.ToList();

            foreach (var mpToken in mpTokenList)
            {
                if (DateTime.Now.AddSeconds(600) >= mpToken.RefreshOn.AddSeconds(mpToken.ExpiredIn))
                {
                    var refreshRlt = ComponentApi.ApiAuthorizerToken(componentToken.ComponentAccessToken, componentAppId,
                                                                     mpToken.MpAppId, mpToken.MpRefreshToken);
                    mpToken.ExpiredIn      = refreshRlt.expires_in;
                    mpToken.MpAccessToken  = refreshRlt.authorizer_access_token;
                    mpToken.MpRefreshToken = refreshRlt.authorizer_refresh_token;
                    mpToken.RefreshOn      = DateTime.Now;
                    _context.SaveChanges();
                }
            }
        }
Beispiel #5
0
        public IHttpActionResult ForceUpdate()
        {
            var componentSvc = new ComponentTokenService();
            var beforeUpdate = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(componentSvc.GetToken()));
            var afterUpdate  = componentSvc.ForceRefresh();
            var result       = new { before = beforeUpdate, after = afterUpdate };

            return(Ok(result));
        }
Beispiel #6
0
        public ActionResult UserAuth(string code, string state, string appid, string returnUrl, string scope)
        {
            try
            {
                if (string.IsNullOrEmpty(appid))
                {
                    return(Content("无效的请求"));
                }

                if (string.IsNullOrEmpty(returnUrl))
                {
                    return(Content("Error: can't find url parameter <b>returnUrl</b>"));
                }

                if (returnUrl.Contains("%") == false)
                {
                    returnUrl = HttpUtility.UrlEncode(returnUrl).Replace("+", "%20");
                }

                //TODO: verify if returnUrl domain is legal or not.

                string componentAppId = ConfigurationManager.AppSettings["AppId"];

                string wxAuthRedirectUri = ConfigurationManager.AppSettings["UserAuthRedirectUri"] + "?returnUrl=" + returnUrl;
                string wxAuthUrlFmt      =
                    "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}&component_appid={3}#wechat_redirect";

                bool silentAuth = !string.IsNullOrEmpty(scope) && scope.ToLower() == "snsapi_base";
                //state is null indicates it's first time to get here.
                if (string.IsNullOrEmpty(state))
                {
                    //TODO: silent user auth here.
                    if (silentAuth)
                    {
                        wxAuthUrlFmt =
                            "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}&component_appid={3}#wechat_redirect";
                        wxAuthRedirectUri += "&scope=snsapi_base";
                    }

                    //throw new Exception("wx based on "+ wxAuthRedirectUri);
                    //第一次进入,跳转到微信授权页
                    string wxAuthUrl = string.Format(wxAuthUrlFmt, appid, HttpUtility.UrlEncode(wxAuthRedirectUri).Replace("+", "%20"),
                                                     "wxAuth1stStep", componentAppId);

                    return(Redirect(wxAuthUrl));
                }

                if (string.IsNullOrEmpty(code))
                {
                    // user reject the auth
                    return(Content("用户未授权,无法继续。"));
                }
                var cts = new ComponentTokenService();
                //通过code换取access_token
                var    componentToken      = cts.GetToken();
                string wxAccessTokenUrlFmt =
                    "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={0}&code={1}&grant_type=authorization_code&component_appid={2}&component_access_token={3}";
                string wxAccessTokenUrl = string.Format(wxAccessTokenUrlFmt, appid, code, componentAppId,
                                                        componentToken.ComponentAccessToken);

                string accessTokenJsonStr = string.Empty;

                HttpClient client = new HttpClient();

                accessTokenJsonStr =
                    client.GetStringAsync(wxAccessTokenUrl)
                    .Result;         //Senparc.CO2NET.HttpUtility.RequestUtility.HttpGet(wxAccessTokenUrl, null);

                var accessTokenJsonObj = JObject.Parse(accessTokenJsonStr);
                var accessCode         = accessTokenJsonObj.GetValue("access_token");
                var openid             = accessTokenJsonObj.GetValue("openid");
                if (openid == null)
                {
                    //log & retry
                    string logmsg = "RETRY: \r\n openid is null \r\n Token Url: " + wxAccessTokenUrl + " \r\n Token info \r\n " +
                                    JsonConvert.SerializeObject(componentToken) + " \r\n accessTokenJsonStr \r\n" +
                                    accessTokenJsonStr;


                    cts.ForceRefresh();
                    componentToken = cts.GetToken();

                    wxAccessTokenUrl = string.Format(wxAccessTokenUrlFmt, appid, code, componentAppId,
                                                     componentToken.ComponentAccessToken);

                    logmsg += "\r\n after update the token url is " + wxAccessTokenUrl;
                    Log(logmsg);

                    //RETRY:
                    accessTokenJsonStr =
                        client.GetStringAsync(wxAccessTokenUrl)
                        .Result;     //Senparc.CO2NET.HttpUtility.RequestUtility.HttpGet(wxAccessTokenUrl, null);

                    accessTokenJsonObj = JObject.Parse(accessTokenJsonStr);
                    accessCode         = accessTokenJsonObj.GetValue("access_token");
                    openid             = accessTokenJsonObj.GetValue("openid");
                }

                if (!silentAuth)
                {
                    //获取用户的基本信息
                    string wxUserInfoUrlFmt =
                        "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN";
                    string wxUserInfoUrl = string.Format(wxUserInfoUrlFmt, accessCode, openid);

                    string userInfoJsonStr = client.GetStringAsync(wxUserInfoUrl).Result; //Senparc.CO2NET.HttpUtility.RequestUtility.HttpGet(wxUserInfoUrl, null);
                    var    userInfoJsonObj = JObject.Parse(userInfoJsonStr);

                    string openIdStr = openid.ToString();

                    string decodeReturnUrl = HttpUtility.UrlDecode(returnUrl);
                    //append infos
                    string redirectUrl = appendUserInfo(decodeReturnUrl, userInfoJsonObj);

                    return(Redirect(redirectUrl));
                }
                else
                {
                    string decodeReturnUrl        = HttpUtility.UrlDecode(returnUrl);
                    bool   alreadyHasUrlParameter = decodeReturnUrl.Contains("?");
                    if (!alreadyHasUrlParameter)
                    {
                        decodeReturnUrl += "?openid=" + openid;
                    }
                    else
                    {
                        decodeReturnUrl += "&openid=" + openid;
                    }
                    return(Redirect(decodeReturnUrl));
                }
            }
            catch (Exception e)
            {
                string msg = e.ToString();
                Log(msg);

                return(Content("请刷新重试"));
            }
        }
Beispiel #7
0
        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));
        }