예제 #1
0
        /// <summary>
        /// 获取oauth信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static QQOauthInfo GetOauthInfo(string code)
        {
            string url = string.Format("https://graph.qq.com/oauth2.0/token?grant_type={0}&client_id={1}&client_secret={2}&code={3}&redirect_uri={4}", "authorization_code", appID, appKey, code, callback);
            string res = GetUrlResponse(url, Encoding.UTF8);

            InfraStructure.Log.InfoLog.Log("SYSTEM", "GetOauthInfo", "GetOauthInfo", res);
            QQOauthInfo qqOauthInfo = new QQOauthInfo();
            var         resultparms = res.Split("&".ToCharArray());
            var         accessToken = string.Empty;

            foreach (var parm in resultparms)
            {
                if (parm.StartsWith("access_token="))
                {
                    qqOauthInfo.AccessToken = parm.Split("=".ToCharArray())[1];
                }
                if (parm.StartsWith("expires_in="))
                {
                    qqOauthInfo.ExpiresIn = parm.Split("=".ToCharArray())[1];
                }
                if (parm.StartsWith("refresh_token="))
                {
                    qqOauthInfo.RefreshToken = parm.Split("=".ToCharArray())[1];
                }
            }
            return(qqOauthInfo);
        }
예제 #2
0
        /// <summary>
        /// 获取QQ账号的OpenID
        /// </summary>
        /// <param name="qqOauthInfo"></param>
        /// <returns></returns>
        public static string GetOpenID(QQOauthInfo qqOauthInfo)
        {
            //PC网站
            string res = GetUrlResponse("https://graph.qq.com/oauth2.0/me?access_token=" + qqOauthInfo.AccessToken, Encoding.UTF8);

            InfraStructure.Log.InfoLog.Log("SYSTEM", "GetOpenID", "GetOpenID", res);
            //callback( { "client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
            string json = res.Substring(res.IndexOf("(") + 1);

            json = json.Substring(0, json.IndexOf(")"));
            dynamic obj = JsonConvert.DeserializeObject(json);

            return(obj.openid);
        }
예제 #3
0
        /// <summary>
        /// 获得用户信息
        /// </summary>
        /// <param name="qqOauthInfo"></param>
        /// <param name="openID"></param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(QQOauthInfo qqOauthInfo, string openID)
        {
            string urlGetInfo  = string.Format(@"https://graph.qq.com/user/get_user_info?access_token={0}&oauth_consumer_key={1}&openid={2}", qqOauthInfo.AccessToken, appID, openID);
            string resUserInfo = GetUrlResponse(urlGetInfo, Encoding.UTF8);

            InfraStructure.Log.InfoLog.Log("SYSTEM", "GetUserInfo", "GetUserInfo", resUserInfo);
            dynamic   obj     = JsonConvert.DeserializeObject(resUserInfo);
            QQAccount qqlogin = new QQAccount()
            {
                nickname = obj.nickname,
                //使用40*40的QQ头像图片作为标准头像
                figureurl  = obj.figureurl_qq_1,
                gender     = obj.gender,
                OpenID     = openID,
                LastAccess = DateTime.Now
            };
            var qqAccount = GetQQAccountByOpenId(qqlogin.OpenID);

            if (qqAccount == null)
            {
                //没有该用户,添加
                var      qqId     = InsertQQAccount(qqlogin);
                UserInfo userinfo = new UserInfo()
                {
                    RegisterAccountID = qqId,
                    TopicList         = new List <string>(),
                    TagList           = new List <string>(),
                    RegisterMethod    = QQ,
                    NickName          = qqlogin.nickname,
                    Avatar_url        = qqlogin.figureurl,
                    Privilege         = UserType.Normal,
                    ContainTag        = string.Empty,
                    AntiTag           = string.Empty,
                    Catalog           = new List <string>(),
                    Level             = new List <ArticleLevel>()
                };
                var userinfoId = UserInfo.InsertUserInfo(userinfo);
                if (userinfoId == 1.ToString(SnFormat))
                {
                    //第一个用户是管理员(该代码仅仅被执行一次)
                    userinfo.Privilege = UserType.Admin;
                    UserInfo.UpdateUserInfo(userinfo);
                }
                //更新UserInfoID
                qqlogin.UserInfoID = userinfoId;
                UpdateQQAccount(qqlogin);

                //Welcome
                var articleurl = "<a href = '/Article/Index?ArticleId=00000006'>网站使用方法</a>";
                SiteMessage.CreateNotify(userinfoId, "欢迎加入CodeSnippet.info,请阅读[" + articleurl + "]");
                return(userinfo);
            }
            else
            {
                //用新的信息替换旧的信息
                qqAccount.figureurl = qqlogin.figureurl;
                qqAccount.nickname  = qqlogin.nickname;
                UpdateQQAccount(qqAccount);

                var userinfo = GetUserInfoByRegMethodInnerId(qqAccount.Sn);
                userinfo.Avatar_url = qqlogin.figureurl;
                userinfo.NickName   = qqlogin.nickname;

                //防御性代码
                if (userinfo.TagList == null)
                {
                    userinfo.TagList = new List <string>();
                }
                if (userinfo.TopicList == null)
                {
                    userinfo.TopicList = new List <string>();
                }
                if (userinfo.Catalog == null)
                {
                    userinfo.Catalog = new List <string>();
                }
                if (userinfo.Level == null)
                {
                    userinfo.Level = new List <ArticleLevel>();
                }
                if (userinfo.ContainTag == null)
                {
                    userinfo.ContainTag = string.Empty;
                }
                if (userinfo.AntiTag == null)
                {
                    userinfo.AntiTag = string.Empty;
                }

                UserInfo.UpdateUserInfo(userinfo);
                return(userinfo);
            }
        }