最近公司需要做一个第三方登录的功能,之前我用JS的SDK玩过。开始的时候,觉得挺简单的,不过本来就简单。
由于DEMO我后面加入QQ的,导致不兼容,出现weibo能登录,QQ的报错,具体不兼容的是Newtonsoft.Json。
Demo搞定了。觉得有点乱,重新把demo里面的东西,重新写了一个解决方案。
准备东西:
1.首先到需要网站上申请AppKey和AppSecret
QQ:http://connect.qq.com/(认证网站)
Sina:http://open.weibo.com/(简单)
AliPay:https://b.alipay.com/order/productDetail.htm?productId=2013052704023441&tabId=4#ps-tabinfo-hash(要签约)
2.设置回调地址
3.在web.config配置对应的key和AppSecret再加回调地址即可。
在这里,很感谢@张善友 提供的QQ SDK,难道疼讯就没有net开发工程师嘛。
这是他的博客园:http://www.cnblogs.com/shanyou/archive/2012/02/05/2338797.html
1.QQ
源码下载地址(由张善友提供):
1.1在这里强烈建议用Nuget来获取,PM> Install-Package QConnectSDK
更新后会有这两个DLL出现在引用里面,下图:
1.2目前我使用的版本:
QConnectSDK 1.1.0.0
RestSharp 104.1.0.0
1.3Controllers
1 /// <summary> 2 /// QQ登录 3 /// </summary> 4 /// <returns>跳转URL</returns> 5 public ActionResult QzoneLogin() 6 { 7 var context = new QzoneContext(); 8 string state = Guid.NewGuid().ToString().Replace("-", ""); 9 string scope = "get_user_info"; 10 var authenticationUrl = context.GetAuthorizationUrl(state, scope); 11 Session["requeststate"] = state; 12 return new RedirectResult(authenticationUrl); 13 } 14 15 /// <summary> 16 /// QQ回调地址 17 /// </summary> 18 /// <param name="code">登录验证CODE</param> 19 /// <returns>返回主页</returns> 20 public ActionResult QzoneCallback(String code) 21 { 22 if (!string.IsNullOrEmpty(code)) 23 { 24 QOpenClient qzone = null; 25 QConnectSDK.Models.User currentUser = null; 26 string state = Session["requeststate"].ToString(); 27 try 28 { 29 qzone = new QOpenClient(code, state); 30 } 31 catch (Exception ex) 32 { 33 34 throw; 35 } 36 currentUser = qzone.GetCurrentUser(); 37 string nickName = currentUser.Nickname; 38 string openId = qzone.OAuthToken.OpenId; 39 string accessToken = qzone.OAuthToken.AccessToken; 40 41 Session["uid"] = openId; 42 Session["nickName"] = nickName; 43 Session["Figureurl"] = currentUser.Figureurl; 44 45 return RedirectToAction("Index"); 46 } 47 return RedirectToAction("Index"); 48 } 49 50 /// <summary> 51 /// 获取用户信息 52 /// </summary> 53 /// <returns></returns> 54 public ActionResult GetQQState() 55 { 56 var json = new JObject(); 57 if (Session["Figureurl"] != null) 58 { 59 60 json["Figureurl"] = Session["Figureurl"].ToString(); 61 62 } 63 return Content(json.ToString(Formatting.None), "application/json"); 64 }
2.Sina
源码(由林选臣提供):
直接引用,暂时没有找到Nuget地址
直接引用这2个DLL即可。
1 /// <summary> 2 /// 封装一个方法来初始化OpenAuth客户端 3 /// </summary> 4 /// <returns></returns> 5 private SinaWeiboClient GetOpenAuthClient() 6 { 7 var accessToken = Session["access_token"] == null ? string.Empty : (string)Session["access_token"]; 8 var uid = Request.Cookies["uid"] == null ? string.Empty : Request.Cookies["uid"].Value; 9 10 var settings = ConfigurationManager.AppSettings; 11 var client = new SinaWeiboClient(settings["Sina_AppKey"], settings["Sina_AppSecret"], settings["Sina_CallbackUrl"], accessToken, uid); 12 13 return client; 14 } 15 /// <summary> 16 /// 获取用户信息 17 /// </summary> 18 /// <returns></returns> 19 public ActionResult GetUserState() 20 { 21 var client = GetOpenAuthClient(); 22 23 if (!client.IsAuthorized) 24 { 25 return Json(new 26 { 27 authorized = false, 28 url = client.GetAuthorizationUrl() 29 }); 30 } 31 32 // 调用获取获取用户信息api 33 // 参考:http://open.weibo.com/wiki/2/users/show 34 var response = client.HttpGet("users/show.json", new 35 { 36 uid = client.UID 37 }); 38 39 if (response.IsSuccessStatusCode) 40 { 41 var json = new JObject(); 42 json["authorized"] = true; 43 json["data"] = JObject.Parse(response.Content.ReadAsStringAsync().Result); 44 return Content(json.ToString(Formatting.None), "application/json"); 45 } 46 else 47 { 48 var json = new JObject(); 49 json["authorized"] = false; 50 json["data"] = JObject.Parse(response.Content.ReadAsStringAsync().Result); 51 52 json["authorized"] = true; 53 return Content(json.ToString(Formatting.None), "application/json"); 54 } 55 } 56 57 /// <summary> 58 /// 授权认证 59 /// </summary> 60 /// <param name="code">新浪返回的code</param> 61 /// <returns></returns> 62 public ActionResult SinaAuthorized(string code) 63 { 64 if (string.IsNullOrEmpty(code)) 65 { 66 return RedirectToAction("Index"); 67 } 68 var client = GetOpenAuthClient(); 69 70 client.GetAccessTokenByCode(code); 71 72 if (client.IsAuthorized) 73 { 74 //用session记录access token 75 Session["access_token"] = client.AccessToken; 76 //用cookie记录uid 77 Response.AppendCookie(new HttpCookie("uid", client.UID) { Expires = DateTime.Now.AddDays(7) }); 78 return RedirectToAction("Index"); 79 } 80 else 81 { 82 return RedirectToAction("Index"); 83 } 84 85 }
3.AliPay(还在申请接口中,待完善)
时间: 2024-10-10 22:47:27