Exemple #1
0
        // GET: ScanCode
        public ActionResult Index()
        {
            string accessToken = WxHelper.GetWXAccessToken(WxConfig.AppId, WxConfig.Secret);

            if (accessToken != "err")
            {
                string jsapi = WxHelper.GetWXJsapi_Ticket(accessToken);
                if (jsapi != "err")
                {
                    LogHelper.WriteFile(Server.MapPath("~/Logs/jsapi.txt"), jsapi);

                    string noncestr  = OperateHelper.GenerateNonceStr();
                    string timestamp = OperateHelper.Timestamp();
                    string url       = WxConfig.CurrentHost + Request.Url.AbsolutePath;
                    string str       = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi, noncestr, timestamp, url);
                    string sign      = OperateHelper.SHA1(str).ToLower();
                    LogHelper.WriteFile(Server.MapPath("~/Logs/jsapi.txt"), str);

                    WxModel wx = new WxModel()
                    {
                        appId     = WxConfig.AppId,
                        nonceStr  = noncestr,
                        timestamp = timestamp,
                        signature = sign,
                    };
                    return(View(wx));
                }
                return(Content("apierr"));
            }
            else
            {
                return(Content("err"));
            }
        }
Exemple #2
0
        public ActionResult Index()
        {
            var     code        = Request.Params["code"];
            var     appid       = ConfigurationManager.AppSettings["AppID"];
            var     secret      = ConfigurationManager.AppSettings["AppSecret"];
            WxModel model       = new WxModel();
            var     ticket      = JsApiTicketContainer.GetJsApiTicket(appid);
            var     noncestr    = JSSDKHelper.GetNoncestr();
            var     timeSpan    = JSSDKHelper.GetTimestamp();
            var     signature   = JSSDKHelper.GetSignature(ticket, noncestr, timeSpan, Request.Url.AbsoluteUri);
            var     accessToken = OAuthApi.GetAccessToken(appid, secret, code);

            if (OAuthApi.Auth(accessToken.access_token, accessToken.openid).errcode == ReturnCode.合法的OpenID)
            {
                accessToken = OAuthApi.RefreshToken(appid, accessToken.refresh_token);
            }
            var accessUserInfo = OAuthApi.GetUserInfo(accessToken.access_token, accessToken.openid);

            model.UnioId    = accessUserInfo.openid;
            model.TimeStamp = timeSpan;
            model.Signature = signature;
            model.NonceStr  = noncestr;

            return(View(model));
        }
Exemple #3
0
        public static WxModel GetWXJsapi()
        {
            string accessToken = WxHelper.GetWXAccessToken(WxConfig.AppId, WxConfig.Secret);

            if (accessToken != "err")
            {
                string jsapi = WxHelper.GetWXJsapi_Ticket(accessToken);
                if (jsapi != "err")
                {
                    string noncestr  = OperateHelper.GenerateNonceStr();
                    string timestamp = OperateHelper.Timestamp();
                    string url       = WxConfig.CurrentHost + System.Web.HttpContext.Current.Request.Url.AbsolutePath;
                    string str       = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi, noncestr, timestamp, url);
                    string sign      = OperateHelper.SHA1(str).ToLower();

                    WxModel wx = new WxModel()
                    {
                        appId     = WxConfig.AppId,
                        nonceStr  = noncestr,
                        timestamp = timestamp,
                        signature = sign,
                    };
                    return(wx);
                }
            }
            return(new WxModel());
        }
        /// <summary>
        /// snsapi_base 只能通过code获取openid,snsapi_userinfo 可通过code获取用户信息
        /// </summary>
        /// <param name="scope"></param>
        /// <returns></returns>
        public ActionResult GetAuthorizeCode(string state)
        {
            string code = Request["code"];

            if (code == null)
            {
                string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={2}#wechat_redirect", WxConfig.AppId, HttpUtility.UrlEncode(WxConfig.CurrentHost + Request.Url.AbsolutePath), state);
                //WxHelper.HttpGetRequest(url);
                return(Redirect(url));
            }
            else
            {
                string  openidUrl  = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code ", WxConfig.AppId, WxConfig.Secret, code);
                string  baseResult = WxHelper.HttpGetRequest(openidUrl);
                WxModel wxModel    = JsonConvert.DeserializeObject <WxModel>(baseResult);
                log.LogHelper.WriteLog("获取access_token", baseResult);
                if (!string.IsNullOrWhiteSpace(wxModel.errcode))
                {
                    return(Content("参数错误"));
                }
                string userInfOUrl    = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN ", wxModel.access_token, wxModel.openid);
                string userInfoResult = WxHelper.HttpGetRequest(userInfOUrl);
                log.LogHelper.WriteLog("获取用户信息", userInfoResult);

                WxUserInfo wxUserInfo = JsonConvert.DeserializeObject <WxUserInfo>(userInfoResult);
                Session["UserInfo"] = wxUserInfo;
                if (!string.IsNullOrWhiteSpace(wxUserInfo.errcode))
                {
                    return(Content("参数错误"));
                }
                return(Content(userInfoResult));
            }
        }
Exemple #5
0
        public ActionResult TestOAuthor(string para)
        {
            var     code   = Request.Params["code"];
            var     appid  = ConfigurationManager.AppSettings["AppID"];
            var     secret = ConfigurationManager.AppSettings["AppSecret"];
            WxModel model  = new WxModel();

            var accessToken = OAuthApi.GetAccessToken(appid, secret, code);

            var accessUserInfo = OAuthApi.GetUserInfo(accessToken.access_token, accessToken.openid);


            //var client = new WebClient();
            //client.Encoding = Encoding.UTF8;

            //var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);
            //var data = client.DownloadString(url);

            //var serializer = new JavaScriptSerializer();
            //var obj = serializer.Deserialize<Dictionary<string, string>>(data);
            //string accessToken;
            //if (!obj.TryGetValue("access_token", out accessToken))
            //    return View();

            //var opentid = obj["openid"];
            //url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, opentid);
            //data = client.DownloadString(url);
            //var userInfo = serializer.Deserialize<Dictionary<string, object>>(data);


            //model.NickName = userInfo["nickname"].ToString();
            //model.UnioId = userInfo["unionid"].ToString();
            model.NickName  = accessUserInfo.nickname;
            model.UnioId    = accessUserInfo.openid;
            model.NonceStr  = OAuthApi.Auth(accessToken.access_token, accessToken.openid).errcode.ToString();
            model.Signature = para;
            return(View(model));
        }