webform获取微信用户的授权

这是一个利用webform做出来的简单demo,微信授权,获取微信用户的基本信息.方便以后加深记忆.

public partial class Index : System.Web.UI.Page
    {
        //开发者appID
        public static string appID = "xxxxxx";
        //
        public static string appsecret = "xxxxx";

        public string a="",b="";


        protected void Page_Load(object sender, EventArgs e)
        {            
            string code = Request["code"];
            if (!string.IsNullOrEmpty(code))
            {
                //获取access_token
                string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" +
                    appsecret + "&code=" + code + "&grant_type=authorization_code";
                JObject jObject = GetMsg(url, true);            
                //从返回的数据中取出openid和access_token来从微信服务器上获取用户信息
                string access_token = jObject["access_token"].ToString();
                string openId = jObject["openid"].ToString();
                string uri= "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
                JObject json = GetMsg(uri,false);
                b = json["nickname"].ToString();
            }
            else
            {
                //对回调网址进行UrlEncode编码
                string uri = HttpUtility.UrlEncode("http:/xxdkys.org/index.aspx");
                //向微信发送token请求
                string url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + "&redirect_uri=" + uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                Response.Redirect(url1);
            }
        }
        
        /// <summary>
        /// 获取url中返回的数据,转换为json对象
        /// </summary>
        /// <param name="url">跳转Uri</param>
        /// <param name="method">postOrGet</param>
        /// <returns></returns>
        JObject GetMsg(string url,bool method)
        {
            //利用HttpWebRequest方法向Url发送请求
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            if (method)
            {

            }
            else
            {
                webRequest.Method = "Get";
            }
            //获取url返回的资源
            WebResponse response = webRequest.GetResponse();
            //利用StreamReader, 为指定的流/文件名初始化 StreamReader 类的新实例,并且使用默认的编码(UTF-8)读取文件
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //利用实例的对象的ReadToEnd()读取所有信息
            JObject jObject = JObject.Parse(reader.ReadToEnd());
            return jObject;
        }
    }

下面这种方法同样可以获取到

        //开发者appID
        public static string appID = "xxxx";
        //开发者appsecret
        public static string appsecret = "xxxxxx";

        public string a="",b="";


        protected void Page_Load(object sender, EventArgs e)
        {            
            string code = Request["code"];
            if (!string.IsNullOrEmpty(code))
            {
                //获取access_token
                string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" +
                    appsecret + "&code=" + code + "&grant_type=authorization_code";                
                WebClient wc = new WebClient();
                wc.Credentials = CredentialCache.DefaultCredentials;
                wc.Encoding = Encoding.UTF8;
                string ruturnText = wc.DownloadString(url);
                JObject jObject = JObject.Parse(ruturnText);
                string access_token = jObject["access_token"].ToString();
                string openId = jObject["openid"].ToString();
                string userInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN";
                a = wc.DownloadString(userInfo);
                JObject json = JObject.Parse(a);
                b = json["nickname"].ToString();
            }
            else
            {
                //对回调网址进行UrlEncode编码
                string uri = HttpUtility.UrlEncode("http://blkys.org/index.aspx");
                //向微信发送token请求
                string url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + "&redirect_uri=" + uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                Response.Redirect(url1);
            }
        }
原文地址:https://www.cnblogs.com/xinqi1995/p/8366304.html