OAuth : OAuth (Open Licensing) is an open standard that allows users to authorize third-party websites to access information they store on other service providers without having to provide their usernames and passwords to third-party websites or to share all the content of their data.
QQ Login OAuth2.0: For user-related OPENAPI (such as access to user information, dynamic synchronization, photos, logs, sharing, etc.), in order to protect the security and privacy of user data, third-party web site access to user data before you need to explicitly request authorization from users.
QQ Login OAuth2.0 uses OAuth2.0 standard protocol to authenticate users and obtain user authorization, and its authentication process is simpler and more secure than the previous OAuth1.0 protocol. Specific reference document: "QQ Login" OAuth2.0 development documentation.
QQ Internet site has provided the Php,js,android and iOS SDK, missing. NET version of the SDK, during the Lunar New Year holidays, a full-featured. NET SDK is packaged with some free time, followed by a corresponding Windows Phone SDK, and open source is placed in the http://opensns.codeplex.com, Specifically built a sample site http://www.win8charm.com/and MSDN style online Help site http://help.win8charm.com/. Today this article mainly introduces the use of the. NET SDK to implement the QQ login function.
From here http://opensns.codeplex.com/download the latest version of the SDK, the latest version is beta, complete the SDK package, I hope you use to help test, The SDK relies on the Newtonsoft.json and Restsharp two assemblies, which you can refer to using the Restsharp library to consume restful Service. The main is two class Qzonecontext (QQ landing context data) and Qopenclient (QQ Interconnection API Portal), other classes are mainly models, configuration classes.
1, you have to go to http://connect.qq.com/to apply for an account, you will get an app ID and app Key, these two things will be used when generating the request. You have to fill in some information, but also to submit some information audit.
In profile Web. config, add some configuration parameters that are required for QQ login, as shown in:
<configuration>
<configsections>
<sectiongroup name= " Qqsectiongroup,
<section name= "qzonesection" type= " System.configuration.namevaluesectionhandler,system, version=4.0.0.0, culture=neutral,publickeytoken= b77a5c561934e089 "/>
</sectiongroup>
</configsections>
<qqsectiongroup>
<qzonesection>
< Add key= "AppKey" value= ""/>
<add key= "Appsecret" value= "/>
<add key= "Callbackuri" value= "/>
<add key=" Authorizeurl "value=" Https://graph.qq.com/oauth2.0/authorize "/>
</QzoneSection>
</qqsectiongroup>
Appkey is to apply for QQ login success, assigned to the application of Appid;appsecret is to apply for the successful QQ login, assigned to the site Appkey Callbackuri is the callback address after the successful QQ landing: Authorizeurl is the OATH2 authentication address of QQ interconnection:
2, add three references Newtonsoft.Json.dll, RestSharp.dll and QConnectSDK.dll in the project, place the button on the page, open the QQ login page, and then callback the page of your website after successful login. At this point, if the user has an account on your website, then you can bind an existing account, or a new account. If you are a new station, you can also use QQ login as the user system completely.
The following code:
<summary>
QQ Landing Page
</summary>
[HttpGet]
Public ActionResult Login (string returnUrl)
{
This. Session[returnurl] = RETURNURL;
var context = new Qzonecontext ();
string state = Guid.NewGuid (). ToString (). Replace ("-", "" ");
session["requeststate"] = State;
string scope = "Get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,add_pic_t,del_t,get_repost_ List,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr ";
var Authenticationurl = context. Getauthorizationurl (State, scope);
return new Redirectresult (Authenticationurl);
}
<summary>
Callback Page
</summary>
Public ActionResult Qqconnect (Loginmodel model)
{
if (request.params["code"]! = NULL)
{
Qopenclient qzone = null;
var verifier = request.params["code"];
var state = request.params["state"];
String requeststate = session["RequestState"]. ToString ();
if (state = = RequestState)
{
Qzone = new Qopenclient (verifier, state);
var currentUser = Qzone. Getcurrentuser ();
if (this. session["Qzoneoauth"] = = null)
{
This. session["Qzoneoauth"] = Qzone;
}
var friendlyname = Currentuser.nickname;
var Ispersistentcookie = true;
SetAuthCookie (Qzone. Oauthtoken.openid, Ispersistentcookie, FriendlyName);
Return Redirect (Url.action ("Index", "Home"));
}
}
return View ();
}
The code above is ASP., the project example runs in http://www.win8charm.com/, and the following code example is affixed with an ASP. NET WebForm:
QQ Landing Page
Namespace OpenConnect.WebSample.Account
{
public partial class LoginToQQ:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Getrequesttoken ();
}
private void Getrequesttoken ()
{
var context = new Qzonecontext ();
string state = Guid.NewGuid (). ToString (). Replace ("-", "" ");
string scope = "Get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,add_pic_t,del_t,get_repost_ List,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr ";
var Authenticationurl = context. Getauthorizationurl (State,scope);
Request token, request token secret need to be saved.
In the demo demo, it is saved directly in the global variable. The real situation needs to be handled by the website itself
session["requeststate"] = State;
Response.Redirect (Authenticationurl);
}
}
}
Callback Page
Namespace OpenConnect.WebSample.Account
{
public partial class QQCallback:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (request.params["code"]! = NULL)
{
Qopenclient qzone = null;
User currentUser = null;
var verifier = request.params["code"];
string state = session["RequestState"]. ToString ();
Qzone = new Qopenclient (verifier, state);
CurrentUser = Qzone. Getcurrentuser ();
if (null! = CurrentUser)
{
This.result.Text = "Successful landing";
This. Nickname.text = Currentuser.nickname;
This. Figureurl.imageurl = Currentuser.figureurl;
}
session["Qzoneoauth"] = Qzone;
}
}
}
Here explain the use of QQ interconnection landing is not to get the user's QQ number, will only get to the user's Openid,openid and QQ number is one by one correspondence.
Local testing
- Prerequisites for understanding the role of the local host file
- Find C:\WINDOWS\system32\drivers\etc\hosts This file
- Open in text mode
- Add one line: 127.0.0.1 www.domain.com
- Start the local server
- Start Browser Access http://www.domain.com/
Other
- Download: http://opensns.codeplex.com/
- Project Example: http://www.win8charm.com/
- SDK online Documentation: http://help.win8charm.com/
- QQ Group: 80767552
- Note: Crowds, you are welcome to feedback the use of the bug.
- Report issue please come to: http://opensns.codeplex.com/workitem/list/basic
- The letter in the station or the following way
- Weibo: Http://t.qq.com/geffzhang
- E-mail: geffzhang#qq.com
- Blog: http://www.cnblogs.com/shanyou
Ext.: http://www.cnblogs.com/shanyou/archive/2012/02/05/2338797.html
QQ Internet OAuth2.0. NET SDK Release and website QQ login sample Code