个人资料

主页 博客 留言板 搜索
跳过导航链接首页 > 博客列表 > 博客正文

ASP.NET接入qq互联第三方登陆处理方案

分类

接上篇ASP.NET网站接入QQ互联过程详解

使用qq互联js sdk进行第三方登陆.使用了自定义登录按钮qqLoginBtn,一个字体图标.

基本过程,其他页面跳转到登录页面时,传递前一页网址originUrl,获取前一页后qq登陆成功Check()后跳转回originUrl.如果originUrl为空,跳回主页.

跳转的时候网址加入query值[openid]和[accessToken].

$(function () {
     $('#qqLoginBtn').click(function () {                   
         QC.Login.showPopup({
             appId: "101381207",
             redirectURI: "https://www.songshizhao.com/account/login.aspx" + "?url="+originUrl
         });
 
     });
 });
 var openId, accessToken;
 if (QC.Login.check()) {
     QC.Login.getMe(function (openId, accessToken) {
         //$.cookie('openId', openId, { expires: 140000000 });
         //$.cookie('accessToken', accessToken, { expires: 140000000 });
         var redirectURI = originUrl + "?openId=" + openId + "&accessToken=" +accessToken;
         window.location.href =redirectURI;
 
     })
 }

登陆成功后,获取到openid和accessToken后,使用网址传值,导航到来页.同时在Asp,net母版页后台代码Page_load中加入代码如下:

if (!IsPostBack)
{
    try
    {
        //var redirectUrl = Convert.ToString(Request.QueryString["url"]);
        var accessToken = Convert.ToString(Request.QueryString["accessToken"]);
        var openId = Convert.ToString(Request.QueryString["openId"]);
 
        if (accessToken.Length > 0 & openId.Length > 0)
        {
            //使用API拉取qq用户信息
            var qqUser = GetQQInfo(openId, accessToken);
            if (qqUser != null)
            {
                var userInfo = user.QQ_info_update_or_regist(openId, qqUser.nickname, qqUser.figureurl_qq_2, "saws1w111ooo");
                if (userInfo != null)
                {
                    HttpContext.Current.Session.Timeout = 480;
                    HttpContext.Current.Session["username"] = userInfo.Username;
                    HttpContext.Current.Session["userphoto"] = userInfo.Photo;
 
                }
                else
                {
                    //Response.Write("<script>alert('使用qq登陆注册用户失败, 原因: 未知');</script>");
                }
 
            }
            else
            {
                //Response.Write("<script>alert('拉取qq用户信息失败');</script>");
            }
        }
    }
    catch (Exception)
    {
    }
}

根据openid和token获取用户信息,判断是否第一次登陆,QQ_info_update_or_regist方法是后台方法判断数据库中是否已存在openid,如存在进行数据更新,如不存在,进行注册.成功后写入session,登陆成功.其中,根据openid和token获取qq信息的代码如下:

public static QQ_User_Info GetQQInfo(string OpenId, string AccessToken)
{
    QQ_User_Info QQ_User = new QQ_User_Info();
    try
    {
        //请求qq互联API
        string InfoUrl = string.Format("https://graph.qq.com/user/get_user_info?access_token={0}&oauth_consumer_key={1}&openid={2}", AccessToken, "101381***", OpenId);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(InfoUrl);
        request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8";
        string JasonString;
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            Stream myResponseStream = response.GetResponseStream();
            using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8))
            {
                JasonString = myStreamReader.ReadToEnd();
            }
        }
        //反序列化json
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        QQ_User = serializer.Deserialize<QQ_User_Info>(JasonString);
        if (QQ_User.ret == "0")//ret=0说明授权成功
        {
            return QQ_User;
            //return user.QQ_info_update_or_regist(OpenId, QQ_User.nickname, QQ_User.figureurl_qq_2, "saws1w111ooo");
        }
        else
        {
            return null;
        }
    }
    catch (Exception)
    {
        return null;
    }
}

songshizhao
最初发表2018/6/6 21:50:28 最近更新2018/6/6 21:50:28 481
为此篇作品打分
10