예제 #1
0
 /// <summary>
 /// 获得微信授权第一步获得code
 /// </summary>
 /// <param name="isbase">是否静默授权,</param>
 /// <param name="backurl">跳转地址</param>
 public static string GetAuthorizeCodeUrl(bool isbase, string backurl)
 {
     try
     {
         //第一步
         string redirect_uri = HttpUtility.UrlEncode(backurl);
         SortedDictionary <string, object> dic = new SortedDictionary <string, object>();
         dic.Add("appid", AppSettingHelper.AppId);
         dic.Add("redirect_uri", redirect_uri);
         dic.Add("response_type", "code");
         dic.Add("scope", isbase ? "snsapi_base" : "snsapi_userinfo");
         dic.Add("state", "STATE" + "#wechat_redirect");//任意值
         string tempurlstr = WXHelper.ToUrl(dic);
         string url        = AppSettingHelper.GetAuthorizeCodeFromUrl + tempurlstr;
         Container.LogService.Info("GetAuthorizeCodeUrl: " + url);
         return(url);
     }
     catch (Exception ex)
     {
         Container.LogService.Error("获取微信授权code错误。\n" + ex.ToString());
         return("");
     }
 }
예제 #2
0
        public static GetOpenIdResponse GetOpenidInfoFromCode(string code)
        {
            try
            {
                SortedDictionary <string, object> dic = new SortedDictionary <string, object>();
                dic["appid"]      = AppSettingHelper.AppId;
                dic["secret"]     = AppSettingHelper.SecretKey;
                dic["code"]       = code;
                dic["grant_type"] = "authorization_code";
                string tempurl = WXHelper.ToUrl(dic);
                string url     = AppSettingHelper.GetAccessTokeFromUrl + tempurl;
                WCFClient.LoggerService.Info("UrlInfo:" + url);
                string result = HttpService.Get(url);

                Container.LogService.Error("GetOpenidInfoFromCode: " + result);
                GetOpenIdResponse objResponse = PublicResource.JsonHelper.ConvertJsonStringToObject <GetOpenIdResponse>(result);
                return(objResponse);
            }
            catch (Exception ex)
            {
                Container.LogService.Error(ex.ToString());
                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// 页面初始化前
        /// </summary>
        protected override void OnPreInit(EventArgs e)
        {
            HttpCookie objCookie = HttpContext.Current.GetCookie(Constants.cookieName_UserInfo);

            if (objCookie != null && objCookie.Values != null && !string.IsNullOrWhiteSpace(objCookie.Values["oi"]))
            {
                NameValueCollection nvc = objCookie.Values;
                _openId            = nvc["oi"].DoTrim();
                _name              = nvc["name"].DoTrim();
                _weixinAccount     = nvc["wa"].DoTrim();
                _platformUserToken = nvc["pfut"].DoTrim();
                _platformUserId    = nvc["pfui"].DoTrim();
                _phoneNumber       = nvc["pn"].DoTrim();
            }
            else
            {
                WCFClient.LoggerService.Info("获取微信OpenId 开始");
                string code  = Request["code"];
                string state = Request["state"];
                if (string.IsNullOrEmpty(code))
                {
                    if (string.IsNullOrWhiteSpace(ToUrl))
                    {
                        return;
                    }
                    string url = WXHelper.GetAuthorizeCodeUrl(true, ToUrl);
                    WCFClient.LoggerService.Info("获取微信OpenId URl" + url);
                    Response.Redirect(url);
                }
                else
                {
                    GetOpenIdResponse ResponseWithOpenId = WXHelper.GetOpenidInfoFromCode(code);
                    WCFClient.LoggerService.Info("获取微信OpenId code" + code);
                    WCFClient.LoggerService.Info("获取微信OpenId state" + state);
                    if (ResponseWithOpenId != null)
                    {
                        try
                        {
                            _openId            = ResponseWithOpenId.openid;
                            _name              = ResponseWithOpenId.openid;
                            _weixinAccount     = string.Empty;
                            _platformUserToken = string.Empty;
                            _platformUserId    = string.Empty;
                            _phoneNumber       = string.Empty;

                            WCFClient.LoggerService.Info("获取微信OpenId OpenId" + _openId);

                            NameValueCollection nvc = new NameValueCollection();
                            nvc["oi"]   = _openId;
                            nvc["name"] = _name;
                            nvc["wa"]   = _weixinAccount;
                            nvc["pfut"] = _platformUserToken;
                            nvc["pfui"] = _platformUserId;
                            nvc["pn"]   = _phoneNumber;
                            HttpContext.Current.AddCookie(Constants.cookieName_UserInfo, nvc, null, HttpContext.Current.Request.Url.Host);
                        }
                        catch (Exception ex)
                        {
                            Container.LogService.Error(ex.ToString());
                        }
                    }
                }
                WCFClient.LoggerService.Info("获取微信OpenId 结束");
            }
        }