首页 > 代码库 > 新浪,QQ,第三方登录

新浪,QQ,第三方登录

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using NetDimension.Weibo;using QConnectSDK;using QConnectSDK.Context;namespace ToothBond.Aiya.Web.Job.Controllers{    public class U3LoginController : Controller    {        Client Sina = null;        OAuth oauth =            new OAuth(            System.Configuration.ConfigurationManager.AppSettings["AppKey"],            System.Configuration.ConfigurationManager.AppSettings["AppSecret"],            System.Configuration.ConfigurationManager.AppSettings["CallbackURL"]);        /// <summary>        /// 新浪登录        /// </summary>        /// <returns></returns>        public ActionResult SinaLogin()        {            Sina = new Client(oauth);            string url = oauth.GetAuthorizeURL();            return new RedirectResult(url);        }        /// <summary>        /// QQ登录        /// </summary>        /// <returns></returns>        public ActionResult QzoneLogin()        {            var context = new QzoneContext();            string state = Guid.NewGuid().ToString().Replace("-", "");            string scope = "get_user_info";            var authenticationUrl = context.GetAuthorizationUrl(state, scope);            HttpContext.Session["requeststate"] = state;            return new RedirectResult(authenticationUrl);        }        /// <summary>        /// 新浪回调        /// </summary>        /// <returns></returns>        public ActionResult SinaCallback()        {            if (!string.IsNullOrEmpty(Request["code"]))            {                Sina = new Client(oauth);                var token = oauth.GetAccessTokenByAuthorizationCode(Request["code"]);                string accessToken = token.Token;                string uid = Sina.API.Entity.Account.GetUID();                var dynamic_userInfo = Sina.API.Dynamic.Users.Show(uid);                //存cookie                TempData["uid"] = uid;                TempData["nickName"] = dynamic_userInfo.screen_name;                //TempData["from"] = dynamic_userInfo.location;                Session["uid"] = uid;                Session["nickName"] = dynamic_userInfo.screen_name;                return Redirect("/");            }            return View();        }        /// <summary>        /// QQ回调        /// </summary>        /// <returns></returns>        public ActionResult QzoneCallback()        {            if (!string.IsNullOrEmpty(Request["code"]))            {                var verifier = Request.Params["code"];                QOpenClient qzone = null;                QConnectSDK.Models.User currentUser = null;                string state = Session["requeststate"].ToString();                qzone = new QOpenClient(verifier, state);                currentUser = qzone.GetCurrentUser();                string nickName = currentUser.Nickname;                string openId = qzone.OAuthToken.OpenId;                string accessToken = qzone.OAuthToken.AccessToken;                Session["uid"] = openId;                Session["nickName"] = nickName;                return Redirect("/");            }            return View();        }    }}