asp.net website to achieve access to QQ Login Sample Code _ Practical Skills

Source: Internet
Author: User

Say not much, directly on the code bar:

  String client_id = "assigned app ID";
  string AppID = "Assigned APP KEY";
  String Return_url = "return processed URL";
  public string Authorize ()
  {
   string state = new Random (100000). Next (99, 99999). 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 the event to call directly Authorize() on it, I was directly with the Jump link processing.

 protected void Page_Load (object sender, EventArgs e)
  { 
   Qq_login q = new Qq_login ();
   String url = q.authorize ();//Here Call
   Response.Write (URL);
  }

Effect:

Click on the image, then call the following method in the URL of the callback, and then look a little further down;

  Public User_info Back_user (string code) {String state = new Random (100000). Next (99, 99999).
   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 ();//Get Access_token//root Access_token Get user 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 obtained here = callback. Substring (callback. IndexOf (' (') + 1, callback. IndexOf (') ')-callback. IndexOf (' (')-1)).
   Trim (); Dynamic JsonP = Jvalue.fromobject (callback); Here I am using a serialized class Library Newtonsoft.Json.dll 6.0.4 version of the helpless company version is only 4.0 version of the proposed use of 6.0.4 version a JsonP = Jsonconvert.deserializeoBject<a> (callback);//newtonsoft.json.dll 4.0 or 4.5 version of the string OpenID = jsonp.openid;//Get user only OpenID//under OpenID The user information can be displayed more with just a few needed to be able to add string GetInfo = string below. 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; }

Click on the image to jump to the specified callback address, and redirect_uri after the address with Authorization 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

Then post the returned class.

  public class User_info
  {public
   string OpenID {get; set;} User 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;}
 }

Summarize

Basically finished, code on so much, interested students hands-on operation can be more familiar with the hope that this article can help.

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.