Asp.net website implements sample code for accessing QQ logon and asp.net sample code

Source: Internet
Author: User

Asp.net website implements sample code for accessing QQ logon and asp.net sample code

Let's talk about the Code:

String client_id = "assigned app id"; string appid = "assigned app key"; string Return_url = "Returned url"; public string Authorize () {string state = new Random (100000 ). next (99,999 99 ). toString (); // random number HttpHelper http = new HttpHelper (); string url = string. format ("https://graph.qq.com/oauth2.0/authorize? Response_type = code & client_id = {0} & redirect_uri = {1} & state = {2} ", client_id, Return_url, state); string str =" <script> location. href = '"+ url +"' </script> "; return str ;}

Click Event to call directlyAuthorize()I can use the jump link directly.

Protected void Page_Load (object sender, EventArgs e) {QQ_Login q = new QQ_Login (); string url = q. Authorize (); // call Response. Write (url) here );}

Effect:

Click the image.urlThe following method is called, and the parameters are further described;

Public User_info Back_User (string code) {string state = new Random (100000 ). next (99,999 99 ). toString (); // random number User_info ui = new User_info (); string url = string. format (" https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id= {0} & client_secret = {1} & code = {2} & redirect_uri = {3} & state = {4} ", client_id, appid, code, Return_url, state ); string uu = HttpHelper. htmlFromUrlGet (url); // processing http request help class string code1 = uu. split ('&') [0]. split ('=') [1]. toString (); // obtain access_token // obtain the user's unique OpenID string url_me = string. format (" https://graph.qq.com/oauth2.0/me?access_token= {0} ", code1); string callback = HttpHelper. htmlFromUrlGet (url_me); // The callback = callback obtained here. substring (callback. indexOf (') + 1, (callback. indexOf (')-callback. indexOf (')-1 )). trim (); // dynamic jsonP = JValue. fromObject (callback); // here I use a serialization class library Newtonsoft. json. dll 6.0.4 version, but the company version is too low, only version 4.0 is recommended to use version 6.0.4 a jsonP = JsonConvert. deserializeObject <a> (callback); // Newtonsoft. json. dll 4.0 or 4.5 string OpenID = jsonP. openid; // obtain the unique OpenID of a user. // obtain the user information based on the OpenID. You can add string getinfo = string below to show more users. format (" https://graph.qq.com/user/get_user_info?access_token= {0} & oauth_consumer_key = {1} & openid = {2} ", code1, client_id, OpenID); string user = HttpHelper. htmlFromUrlGet (getinfo); B info = JsonConvert. deserializeObject <B> (user); ui. name = info. nickname; ui. openID = OpenID; ui. img_qq100 = info. figureurl_qq_1; ui. img_qq50 = info. figureurl_qq_2; ui. city = info. city; ui. year = info. year; ui. type = 1; return ui ;}

When you click an image, jump to the specified callback address and goredirect_uriAddressAuthorization Code;

User_info ui = new User_info (); var verifier = Request. Params ["code"]; // get Authorization Code ui = q. Back_User (verifier); drop method and return data

Paste the returned class.

Public class User_info {public string OpenID {get; set;} // the unique appid public string Name {get; set;} public string img_qq50 {get; set ;} // QQ image 40*40 public string img_qq100 {get; set;} public string city {get; set;} public int year {get; set ;}}
 public class a {   public string client_id { get; set; }   public string openid { get; set; } }

Summary

This is basically the end of the process, and there is so much code that interested users can be more familiar with their own operations. I hope this article will help you.

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.