Qq Internet oauth2.0. Net SDK released and sample code for QQ login on the website

Source: Internet
Author: User
Tags oauth openid

Oauth: Oauth is an open standard that allows users to authorize third-party websites to access the information they store on another service provider, instead of providing usernames and passwords to third-party websites or sharing all their data.

Log on to oauth2.0 via QQ: Openapis related to users (such as getting user information, dynamic synchronization, photos, logs, and sharing) are used to protect the security and privacy of user data, before a third-party website accesses user data, it must explicitly ask the user for authorization.
QQ logs on to oauth2.0 using the oauth2.0 standard protocol for user authentication and user authorization. Compared with the previous oauth1.0 protocol, the authentication process is simpler and safer. For more information, see [QQ logon] oauth2.0 development document.

The QQ Internet station has provided PHP, JS, Android, and iOS sdks, which are missing.. Net SDK. during the Spring Festival holiday, a fully functional SDK is encapsulated using some idle time. net SDK, later will encapsulate a corresponding Windows Phone SDK, and open source on the http://opensns.codeplex.com, dedicated to build a sample site http://www.win8charm.com/and msdn style online help site http://help.win8charm.com /. This article introduces how to use. Net SDK to implement QQ login.

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 can use help test, SDK depends on newtonsoft. for details about the two sets of JSON and restsharp, see use the restsharp library to consume restful services. There are two main types of qzonecontext (qq login context data) and qopenclient (qq interconnection API portal). The other types are mainly models and configuration classes.

1. You have to go to http://connect.qq.com/apply for an account, will get an app ID and app key, these two stuff will be used when generating the request. You have to fill in some materials and submit some materials for review.

Add some configuration parameters required for QQ login in the configuration file web. config, 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 the appid assigned to the application after successfully applying for QQ login; appsecret is the appkey assigned to the website after the QQ login is successful; callbackuri is the callback address after the QQ login is successful: authorizeurl is the oath2 authentication address of QQ Internet:

2. Add three references to newtonsoft in the project. JSON. DLL, restsharp. DLL and qconnectsdk. DLL, place a button on the page to open the QQ login page, and then call back the page of your website after successful logon. If you have an account on your website, you can bind an existing account or register a new account. If you are creating a new website, you can also use QQ logon as the user system.

The code below:

/// <Summary>
/// QQ login 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, upload, upload_pic, upload, add_t, expiration, del_t, expiration, get_info, expiration, get_fanslist, get_idolist, add_idol, del_idol, comment, 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 above code is ASP. net mvc, the project example runs in the http://www.win8charm.com/, the following is a sample code ASP. NET webform:

Qq login page

Namespace openconnect. websample. Account
{
Public partial class logstores QQ: 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, upload, upload_pic, upload, add_t, expiration, del_t, expiration, get_info, expiration, get_fanslist, get_idolist, add_idol, del_idol, comment, add_topic, get_tenpay_addr ";
VaR authenticationurl = context. getauthorizationurl (State, scope );
// Request token and request token secret need to be saved
// In the demo, save it directly in the global variable. The actual situation must be handled by the website.
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 = "successfully logged on ";
This. Nickname. Text = currentuser. Nickname;
This. figureurl. imageurl = currentuser. figureurl;
}
Session ["qzoneoauth"] = Qzone;

}

}

}

Here, we will describe that using QQ to log on via the Internet cannot obtain the user's QQ number. Only the user's openid is obtained, and the openid and QQ number are one-to-one mappings.

Local Test
  • Prerequisites: understand the role of the local host file
  1. Find the file c: \ windows \ system32 \ drivers \ etc \ hosts.
  2. Open in text
  3. Add a row: 127.0.0.1 www.domain.com
  4. Start the local server
  5. Start the browser to access http://www.domain.com/
Others
  • Download: http://opensns.codeplex.com/
  • Project Example: http://www.win8charm.com/
  • SDK online documentation: http://help.win8charm.com/
  • QQ: 80767552
  • Note: The fire is high. You are welcome to report the bugs in use.
  • Report issue, http://opensns.codeplex.com/workitem/list/basic
  • Insite email or the following method
  • Weibo: http://t.qq.com/geffzhang
  • Email: geffzhang # QQ.com
  • Blog: http://www.cnblogs.com/shanyou
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.