Пример #1
0
        public ActionResult Callback()
        {
            var code = Request.QueryString.Get("code");

            if (string.IsNullOrEmpty(code))//没有code表示授权失败
            {
                return(RedirectToAction("Failed", "OAuth"));
            }
            var     state              = Request.QueryString.Get("state");
            var     cache_status       = System.Web.HttpContext.Current.Cache.Get(state);
            var     redirect_url       = cache_status == null ? "/" : cache_status.ToString();//没有获取到state,就跳转到首页
            var     access_token_scope = "";
            double  expires_in         = 0;
            var     access_token       = "";
            var     openId             = "";
            var     token              = OAuth2API.GetAccessToken(code, WeixinConfig.AppID, WeixinConfig.AppSecret);
            dynamic userinfo;

            var refreshAccess_token = OAuth2API.RefreshAccess_token(token.refresh_token, WeixinConfig.AppID);

            access_token       = refreshAccess_token.access_token;//通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。
            openId             = refreshAccess_token.openid;
            access_token_scope = refreshAccess_token.scope;
            expires_in         = refreshAccess_token.expires_in;
            userinfo           = OAuth2API.GetUserInfo(access_token, openId);//snsapi_userinfo,可以用户在未关注公众号的情况下获取用户基本信息

            //写入cookies
            AuthorizationManager.SetTicket(true, 1, openId, userinfo.nickname);
            Thread.Sleep(500);//暂停半秒钟,以等待IOS设置Cookies的延迟
            LogWriter.Default.WriteInfo(string.Format("OAuth success: identity: {0} , name: {1} , redirect_rul:{2} , expires_in: {3}s ", openId, userinfo.nickname, redirect_url, expires_in));
            return(new RedirectResult(redirect_url, true));
        }
Пример #2
0
        public ActionResult Callback()
        {
            var code = Request.QueryString.Get("code");

            //没有code表示授权失败
            if (string.IsNullOrEmpty(code))
            {
                return(RedirectToAction("Failed", "OAuth"));
            }
            var state        = Request.QueryString.Get("state");
            var cache_status = System.Web.HttpContext.Current.Cache.Get(state);
            //没有获取到state,就跳转到首页
            var redirect_url = cache_status == null ? "/" : cache_status.ToString();

            redirect_url = HttpUtility.UrlDecode(redirect_url);
            var    scope = WeixinConfig.OauthScope;
            var    access_token_scope = "";
            double expires_in         = 0;
            var    access_token       = "";
            var    openId             = "";
            var    token = OAuth2API.GetAccessToken(WeixinConfig.AppID, WeixinConfig.AppSecret, code);

            dynamic userinfo;

            if (scope == "snsapi_userinfo")
            {
                //var refreshAccess_token = OAuth2API.RefreshAccess_token(token.refresh_token, WeixinConfig.AppID);
                ////通过code换取的是一个特殊的网页授权access_token,
                ////与基础支持中的access_token(该access_token用于调用其他接口)不同。
                //access_token = refreshAccess_token.access_token;
                //openId = refreshAccess_token.openid;
                //access_token_scope = refreshAccess_token.scope;
                //expires_in = refreshAccess_token.expires_in;
                //snsapi_userinfo,可以用户在未关注公众号的情况下获取用户基本信息
                userinfo = OAuth2API.GetUserInfo(token.access_token, token.openid);
            }
            else
            {
                //基础支持中的access_token
                access_token = WeixinConfig.AccessTokenHelper.GetToken();
                openId       = token.openid;
                expires_in   = token.expires_in;
                //TODO: 如果用户已经关注,可以用openid,获取用户信息。
                //如果本地已经存储了用户基本信息,建议在本地获取。
                userinfo = AdminAPI.GetUserInfo(access_token, openId);
            }
            Log.Instence.LogWriteLine(userinfo.JsonString);
            if (userinfo != null)
            {
                redirect_url += "?unionId=" + userinfo.unionid;
            }
            //写入cookies
            AuthorizationManager.SetTicket(true, 1, openId, userinfo.nickname);
            Thread.Sleep(500);//暂停半秒钟,以等待IOS设置Cookies的延迟
            Log.Instence.LogWriteLine(string.Format("OAuth success: identity: {0} , name: {1} , redirect_rul:{2} , expires_in: {3}s ", openId, userinfo.nickname, redirect_url, expires_in));
            return(new RedirectResult(redirect_url, true));
        }
Пример #3
0
        public ActionResult SignUp()
        {
            var code = Request.QueryString.Get("code");

            if (string.IsNullOrEmpty(code))
            {
                throw new UserFriendlyException("授权失败");
            }
            var     access_token_scope = "";
            double  expires_in         = 0;
            var     access_token       = "";
            var     openId             = "";
            var     appId     = "wx25750ab6611b4901";
            var     appSecret = "f178fca842e26a3563e168f7bfb15e58";
            var     token     = OAuth2API.GetAccessToken(code, appId, appSecret);
            dynamic userinfo;

            var refreshAccess_token = OAuth2API.RefreshAccess_token(token.refresh_token, appId);

            //var client = new HttpClient();
            //var result = client.GetAsync(string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", appId, token.refresh_token)).Result;
            //if (!result.IsSuccessStatusCode) return null;
            //Logger.Debug(result.Content.ReadAsStringAsync().Result);


            access_token       = refreshAccess_token.access_token;
            openId             = refreshAccess_token.openid;
            access_token_scope = refreshAccess_token.scope;
            expires_in         = refreshAccess_token.expires_in;

            //Logger.Debug(access_token);

            userinfo = OAuth2API.GetUserInfo(access_token, openId);
            var user   = new User {
            };
            var openid = userinfo.openid;

            if (userinfo != null && !string.IsNullOrEmpty(openid))
            {
                Logger.Debug("openid is" + openid);
                var u = this._iUserAppService.GetUser(openid);
                if (u != null)
                {
                    user = u;
                }

                user.HeadImgUrl = userinfo.headimgurl;
                user.NickName   = userinfo.nickname;
                user.OpenId     = userinfo.openid;
                user.Sex        = Convert.ToInt32(userinfo.sex);
                user.City       = userinfo.city;
                user.Province   = userinfo.province;
                user.Country    = userinfo.country;
                this._iUserAppService.SaveUserInfo(user);
                ViewBag.UserId = user.Id;
            }
            else
            {
                throw new UserFriendlyException("授权失败");
            }
            return(View());
        }