WeChat development-obtain basic user information through authorization

Source: Internet
Author: User
Enter wx.alinq.org. After obtaining the user's authorization, the page will jump to a page specified by the developer. the link to this page must be under this domain name. If not entered, a page with invalid link will appear. 1. enter the domain name on the authorization callback page

Enter wx.alinq.org. After obtaining the user's authorization, the page will jump to a page specified by the developer. the link to this page must be under this domain name. If not entered, a page with invalid link will appear.

III. complete implementation code

The following is the complete code, which is useful to you. Pai_^

<%@ WebHandler Language="C#" %>public class UserAuth : IHttpHandler{    public void ProcessRequest(HttpContext context)    {         var appid = "wxf1c24c60e3ac13b7";        var secret = "5902b9817acb7a290d4b7c2e6e97d4d3";        var code = context.Request.QueryString["Code"];        if (string.IsNullOrEmpty(code))        {            var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fwx.alinq.org%2fTest%2fUserAuth.ashx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", appid);            context.Response.Redirect(url);        }        else        {            var client = new System.Net.WebClient();            client.Encoding = System.Text.Encoding.UTF8;            var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);            var data = client.DownloadString(url);            var serializer = new JavaScriptSerializer();            var obj = serializer.Deserialize
 
  >(data);            string accessToken;            if (!obj.TryGetValue("access_token", out accessToken))                return;            var opentid = obj["openid"];            url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, opentid);            data = client.DownloadString(url);            var userInfo = serializer.Deserialize
  
   >(data);            foreach (var key in userInfo.Keys)            {                context.Response.Write(string.Format("{0}: {1}", key, userInfo[key]) + "
   
"); } } }}

More development-get the user's basic information through authorization. For more information, see PHP!

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.