Example #1
0
        private void sina(oauth_config config)
        {
            string access_token = string.Empty;
            string expires_in   = string.Empty;
            string client_id    = string.Empty;
            string openid       = string.Empty;
            //取得返回参数
            string state = DTRequest.GetQueryString("state");
            string code  = DTRequest.GetQueryString("code");

            //第一步:获取Access Token
            Dictionary <string, object> dic = sina_helper.get_access_token(config.oauth_app_id, config.oauth_app_key, config.return_uri, code);

            if (dic == null || !dic.ContainsKey("access_token"))
            {
                Response.Write("出错了,无法获取Access Token,请检查App Key是否正确!");
                return;
            }

            access_token = dic["access_token"].ToString();
            expires_in   = dic["expires_in"].ToString();
            openid       = dic["uid"].ToString();
            //储存获取数据用到的信息
            Session["site_oauth_id"]      = config.oauth_id;
            Session["oauth_name"]         = config.oauth_name;
            Session["oauth_access_token"] = access_token;
            Session["oauth_openid"]       = openid;

            //第二步:跳转到指定页面
            Response.Redirect(new Web.UI.BasePage().getlink(config.site_path, new Web.UI.BasePage().linkurl("oauth_login")));
            return;
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //获得配置信息
            int site_oauth_id = DTRequest.GetQueryInt("id"); //站点的OAuthID

            if (site_oauth_id == 0)
            {
                Response.Write("出错了,当前站点的OAuth参数有误!");
                return;
            }
            oauth_config config = oauth_helper.get_config(site_oauth_id); //获取OAuth应用信息

            if (config == null)
            {
                Response.Write("出错了,获取当前站点的OAuth参数有误!");
                return;
            }
            string state = Guid.NewGuid().ToString().Replace("-", "");

            Session["oauth_state"]   = state;         //防止CSRF攻击
            Session["site_oauth_id"] = site_oauth_id; //当前站点的OAuthID
            string send_url = string.Empty;           //要跳转的网址

            switch (config.oauth_name)
            {
            case "qq":
                send_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=get_user_info,get_info";
                break;

            case "sina":
                send_url = "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri);
                break;

            case "weixin":
                send_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + config.oauth_app_id + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";
                break;

            case "renren":
                send_url = "https://graph.renren.com/oauth/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=read_user_share read_user_feed";
                break;

            case "kaixin":
                send_url = "http://api.kaixin001.com/oauth2/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=basic user_birthday";
                break;

            case "feixin":
                send_url = "https://i.feixin.10086.cn/oauth2/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=basic";
                break;
            }
            if (!string.IsNullOrEmpty(send_url))
            {
                Response.Redirect(send_url); //跳转到该网址
            }
            else
            {
                Response.Write("出错了,无法找到该OAuth应用接口!");
                return;
            }
        }
Example #3
0
        private void qq(oauth_config config)
        {
            string access_token = string.Empty;
            string expires_in   = string.Empty;
            string client_id    = string.Empty;
            string openid       = string.Empty;
            //取得返回参数
            string code  = DTRequest.GetQueryString("code");
            string state = DTRequest.GetQueryString("state");

            //第一步:获取Access Token
            Dictionary <string, object> dic1 = qq_helper.get_access_token(config.oauth_app_id, config.oauth_app_key, config.return_uri, code, state);

            if (dic1 == null || !dic1.ContainsKey("access_token"))
            {
                Response.Write("错误代码:,无法获取Access Token,请检查App Key是否正确!");
                return;
            }
            access_token = dic1["access_token"].ToString();
            expires_in   = dic1["expires_in"].ToString();

            //第二步:通过Access Token来获取用户的OpenID
            Dictionary <string, object> dic2 = qq_helper.get_open_id(access_token);

            if (dic2 == null || !dic2.ContainsKey("openid"))
            {
                if (dic2.ContainsKey("error"))
                {
                    Response.Write("error:" + dic2["error"] + ",error_description:" + dic2["error_description"]);
                }
                else
                {
                    Response.Write("出错啦,无法获取用户授权Openid!");
                }
                return;
            }
            client_id = dic2["client_id"].ToString();
            openid    = dic2["openid"].ToString();
            //储存获取数据用到的信息
            Session["site_oauth_id"]      = config.oauth_id;
            Session["oauth_name"]         = config.oauth_name;
            Session["oauth_access_token"] = access_token;
            Session["oauth_openid"]       = openid;

            //第三步:跳转到指定页面
            Response.Redirect(new Web.UI.BasePage().getlink(config.site_path, new Web.UI.BasePage().linkurl("oauth_login")));
            //Response.Write("\access_token:" + access_token + ",openid:" + openid);
            return;
        }
Example #4
0
 protected void Page_Load(object sender, EventArgs e)
 {
     //获得配置信息
     oauth_config config = oauth_helper.get_config("kaixin");
     if (config == null)
     {
         Response.Write("出错了,您尚未配置开心网的API信息!");
         return;
     }
     string state = Guid.NewGuid().ToString().Replace("-", "");
     Session["oauth_state"] = state;
     string send_url = "http://api.kaixin001.com/oauth2/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=basic user_birthday";
     //开始发送
     Response.Redirect(send_url);
 }
Example #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            oauth_config config = oauth_helper.get_config("taobao");

            if (config == null)
            {
                this.Response.Write("出错了,您尚未配置飞信的API信息!");
            }
            else
            {
                string str = Guid.NewGuid().ToString().Replace("-", "");
                this.Session["oauth_state"] = (object)str;
                this.Response.Redirect("https://oauth.taobao.com/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + str + "&redirect_uri=" + Utils.UrlEncode(config.return_uri));
            }
        }
Example #6
0
 protected void Page_Load(object sender, EventArgs e)
 {
     //获得配置信息
     oauth_config config = oauth_helper.get_config("renren");
     if (config == null)
     {
         Response.Write("出错了,您尚未配置QQ互联的API信息!");
         return;
     }
     string state = Guid.NewGuid().ToString().Replace("-", "");
     Session["oauth_state"] = state;
     string send_url = "https://graph.renren.com/oauth/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&scope=read_user_share read_user_feed";
     //开始发送
     Response.Redirect(send_url);
 }
Example #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["oauth_state"] == null || Session["site_oauth_id"] == null)
            {
                Response.Write("出错了,无法获取Session信息!");
                return;
            }
            //取得返回参数
            string state = DTRequest.GetQueryString("state");

            if (state != Session["oauth_state"].ToString())
            {
                Response.Write("出错了,state未初始化!");
                return;
            }

            int          site_oauth_id = Utils.StrToInt(Session["site_oauth_id"].ToString(), 0); //获取站点OAuthID
            oauth_config config        = oauth_helper.get_config(site_oauth_id);                 //获取配置信息

            switch (config.oauth_name)
            {
            case "qq":
                qq(config);
                break;

            case "sina":
                sina(config);
                break;

            case "weixin":
                weixin(config);
                break;

            case "renren":
                renren(config);
                break;

            case "kaixin":
                kaixin(config);
                break;

            case "feixin":
                feixin(config);
                break;
            }
        }
Example #8
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //獲得配置資料
            oauth_config config = oauth_helper.get_config("sina");

            if (config == null)
            {
                Response.Write("出錯了,您尚未配置新浪微博的API資料!");
                return;
            }
            string state = Guid.NewGuid().ToString().Replace("-", "");

            Session["oauth_state"] = state;
            string send_url = "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=" + config.oauth_app_id + "&state=" + state + "&redirect_uri=" + Utils.UrlEncode(config.return_uri);

            //開始發送
            Response.Redirect(send_url);
        }
Example #9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //获得配置信息
            oauth_config config = oauth_helper.get_config("weixin");

            if (config == null)
            {
                Response.Write("出错了,您尚未配置微信的API信息!");
                return;
            }
            string state = Guid.NewGuid().ToString().Replace("-", "");

            Session["oauth_state"] = state;
            string send_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + config.oauth_app_id + "&redirect_uri=" + Utils.UrlEncode(config.return_uri) + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";

            //开始发送
            Response.Redirect(send_url);
        }
Example #10
0
        private void kaixin(oauth_config config)
        {
            string access_token = string.Empty;
            string expires_in   = string.Empty;
            string client_id    = string.Empty;
            string openid       = string.Empty;
            //取得返回参数
            string state = DTRequest.GetQueryString("state");
            string code  = DTRequest.GetQueryString("code");

            //第一步:获取Access Token
            Dictionary <string, object> dic1 = kaixin_helper.get_access_token(config.oauth_app_id, config.oauth_app_key, config.return_uri, code, state);

            if (dic1 == null)
            {
                Response.Write("出错了,无法获取Access Token,请检查App Key是否正确!");
                return;
            }
            access_token = dic1["access_token"].ToString();
            expires_in   = dic1["expires_in"].ToString();

            //第二步:通过Access Token来获取用户的ID
            Dictionary <string, object> dic2 = kaixin_helper.get_info(access_token, "uid");

            if (dic2 == null)
            {
                Response.Write("出错啦,无法获取用户授权uid!");
                return;
            }
            openid = dic2["uid"].ToString();
            //储存获取数据用到的信息
            Session["site_oauth_id"]      = config.oauth_id;
            Session["oauth_name"]         = config.oauth_name;
            Session["oauth_access_token"] = access_token;
            Session["oauth_openid"]       = openid;

            //第三步:跳转到指定页面
            Response.Redirect(new Web.UI.BasePage().getlink(config.site_path, "oauth_login"));
            return;
        }
Example #11
0
        private void renren(int site_oauth_id, string oauth_name, string oauth_access_token, string oauth_openid)
        {
            oauth_config config             = oauth_helper.get_config(site_oauth_id); //获取参数信息
            Dictionary <string, object> dic = renren_helper.get_info(config.oauth_app_key, oauth_access_token, "uid,name,sex,birthday,mainurl");

            if (dic == null)
            {
                Response.Write("{\"ret\":\"1\", \"msg\":\"出错啦,无法获取授权用户信息!\"}");
                return;
            }

            StringBuilder str = new StringBuilder();

            str.Append("{");
            str.Append("\"ret\": \"0\", ");
            str.Append("\"msg\": \"获得用户信息成功!\", ");
            str.Append("\"oauth_name\": \"" + oauth_name + "\", ");
            str.Append("\"oauth_access_token\": \"" + oauth_access_token + "\", ");
            str.Append("\"oauth_openid\": \"" + dic["uid"].ToString() + "\", ");
            str.Append("\"nick\": \"" + dic["name"].ToString() + "\", ");
            str.Append("\"avatar\": \"" + dic["mainurl"].ToString() + "\", ");
            if (dic["sex"].ToString() == "1")
            {
                str.Append("\"sex\": \"男\", ");
            }
            else if (dic["sex"].ToString() == "0")
            {
                str.Append("\"sex\": \"女\", ");
            }
            else
            {
                str.Append("\"sex\": \"保密\", ");
            }
            str.Append("\"birthday\": \"" + dic["birthday"].ToString() + "\"");
            str.Append("}");

            Response.Write(str.ToString());
            return;
        }
Example #12
0
        private void renren(oauth_config config)
        {
            string access_token = string.Empty;
            string expires_in   = string.Empty;
            string openid       = string.Empty;
            //取得返回参数
            string state = DTRequest.GetQueryString("state");
            string code  = DTRequest.GetQueryString("code");

            if (string.IsNullOrEmpty(code))
            {
                Response.Write("授权被取消,相关信息:" + DTRequest.GetQueryString("error"));
                return;
            }

            //获取Access Token
            Dictionary <string, object> dic = renren_helper.get_access_token(config.oauth_app_id, config.oauth_app_key, config.return_uri, code);

            if (dic == null)
            {
                Response.Write("错误代码:,无法获取Access Token,请检查App Key是否正确!");
            }

            access_token = dic["access_token"].ToString();
            expires_in   = dic["expires_in"].ToString();
            Dictionary <string, object> dic1 = dic["user"] as Dictionary <string, object>;

            openid = dic1["id"].ToString();
            //储存获取数据用到的信息
            Session["site_oauth_id"]      = config.oauth_id;
            Session["oauth_name"]         = config.oauth_name;
            Session["oauth_access_token"] = access_token;
            Session["oauth_openid"]       = openid;

            //跳转到指定页面
            Response.Redirect(new Web.UI.BasePage().getlink(config.site_path, new Web.UI.BasePage().linkurl("oauth_login")));
            return;
        }
Example #13
0
        private void qq(int site_oauth_id, string oauth_name, string oauth_access_token, string oauth_openid)
        {
            oauth_config config             = oauth_helper.get_config(site_oauth_id); //获取参数信息
            Dictionary <string, object> dic = qq_helper.get_user_info(config.oauth_app_id, oauth_access_token, oauth_openid);

            if (dic == null)
            {
                Response.Write("{\"ret\":\"1\", \"msg\":\"出错啦,无法获取授权用户信息!\"}");
                return;
            }
            try
            {
                if (dic["ret"].ToString() != "0")
                {
                    Response.Write("{\"ret\":\"" + dic["ret"].ToString() + "\", \"msg\":\"出错信息:" + dic["msg"].ToString() + "!\"}");
                    return;
                }
                StringBuilder str = new StringBuilder();
                str.Append("{");
                str.Append("\"ret\": \"" + dic["ret"].ToString() + "\", ");
                str.Append("\"msg\": \"" + dic["msg"].ToString() + "\", ");
                str.Append("\"oauth_name\": \"" + oauth_name + "\", ");
                str.Append("\"oauth_access_token\": \"" + oauth_access_token + "\", ");
                str.Append("\"oauth_openid\": \"" + oauth_openid + "\", ");
                str.Append("\"nick\": \"" + dic["nickname"].ToString() + "\", ");
                str.Append("\"avatar\": \"" + dic["figureurl_qq_2"].ToString() + "\", ");
                str.Append("\"sex\": \"" + dic["gender"].ToString() + "\", ");
                str.Append("\"birthday\": \"\"");
                str.Append("}");
                Response.Write(str.ToString());
            }
            catch
            {
                Response.Write("{\"ret\":\"1\", \"msg\":\"出错啦,无法获取授权用户信息!\"}");
            }
            return;
        }