首页 > 代码库 > 新浪,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(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。