Use QQ to log on to the website

Source: Internet
Author: User
Tags call back

My friend told me last few days that he knew how to use the QQ number to log on to or register the website. So he asked him to send it to me for study. It felt so complicated, moreover, it cannot run (mainly because it is an experiment to upload this item to the space), so I found relevant information in the blog garden and found that it is not very complicated, it's much simpler than him.

First download the latest SDK: http://opensns.codeplex.com/

The SDK depends on newtonsoft. JSON and restsharp.ProgramSet. For more information, 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.

My new project is in. Net framwork 4.0,Convert to. Net framwork 4.0. Otherwise, an error may occur and the task cannot run.

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,The followingCodeAddSuch as the selected place

 

<Sectiongroup name = "  Qqsectiongroup " >
<Section name = " Qzonesection " Type = " System. configuration. namevaluesectionhandler, system, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 " />
</Sectiongroup>
<Section name = " Entityframework " Type = " System. Data. entity. Internal. configfile. entityframeworksection, entityframework, version = 4.3.0.0, culture = neutral, publickeytoken = b77a5c561934e089 " />
</Configsections>
<Qqsectiongroup>
<Qzonesection>
<Add key = " Appkey " Value = " ******* " /> <! -- Here you fill in http: // Connect.qq.com/manage/id of the app registered after registration -->
<Add key = " Appsecret " Value = " ***************** " /> <! -- Enter the registered app key here -->
<Add key = " Callbackuri " Value = " Http://www.xxxx.cn " /> <! -- Callback address written during registration -->
<Add key = " Authorizeurl " Value = " Https://graph.qq.com/oauth2.0/authorize " />
</Qzonesection>
</Qqsectiongroup>
<Deleetask>
<! -- <Add key = " Clientvalidationenabled " Value =" True " />
<Add key = " Unobtrusivejavascriptenabled " Value = " True " />
<Add key = " Daotype " Value = " Codingwell. devtext. sqldal " /> -->
<Add key = " Appkey " Value = " ************* " /> <! -- Here is the same as above -->
<Add key = " Appsecret " Value = " ******************* " />
<Add key = " Callbackurl " Value = " Http://www.xxxxxxxxx.cn " />
<Add key = " Smtphost " Value = " Smtp.gmail.com " />
<Add key = " Smtpuser " Value = "" />
<Add key = " Smtppassword " Value = "" />
<Add key = " Enablessl " Value = " True " />
<Add key =" Sslport " Value = " 587 " />
</Appsettings>

 

Appkey is the appid assigned to the application after successfully applying for a QQ login; appsecret is the appkey assigned to the website after successfully applying for a QQ login; callbackuri is the callback address after successful QQ login (the return address is the callback address entered in the following figure when you apply for qq); authorizeurl is the oath2 authentication address of QQ.

(This reason is becauseOne step forward is happinessSend a message to me and say, "What is the callback URL of callbackuri?", so we have added an explanation. If other friends do not understand anything, please leave a message for me, or Add the following QQ Group to ask me !)

Add the three references newtonsoft In the downloaded SDK folder to 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.

Login Page:

 Using Qconnectsdk. context; //  Please join this namespace  

Public Partial Class Logtailqq: 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, response, upload_pic, response, add_t, response, del_t, response, get_info, response, get_fanslist, get_idolist, add_idol, del_idol, add_one_blog, add_topic " ;
VaR Authenticationurl = context. getauthorizationurl (State, scope );
// Request token and request token secret need to be saved
// In the demo, it is saved directly in the global variable. The actual situation must be handled by the website.
Session [" Requeststate " ] = State;
Response. Redirect (authenticationurl );

}


}

Callback page:

View code

 Using Qconnectsdk; //  Please join this namespace  
Using Qconnectsdk. models;
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 = " Login successful " ;
This . Nickname. Text = currentuser. Nickname;
This . Figureurl. imageurl = currentuser. figureurl;
}
Session [ " Qzoneoauth " ] = Qzone;
}
}
}
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. ****. com the URL you wrote during registration is not the callback address (but maybe your callback address is the same as your url). This is the so-called domain name hijacking!
    4. Start the local server
    5. Start the browser to access http: // www. *****. com/

Reference blog: http://www.cnblogs.com/shanyou/archive/2012/02/05/2338797.html

Http://www.devtext.com/blogs/detail/43/devtext-com-source-code and the website provides download site source code.

This example Demo: http://files.cnblogs.com/koeltp/%E7%BD%91%E7% AB %99%E4%BD%BF%E7%94%A8QQ%E7%99%BB%E9%99%86.rar

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.