.net 实现微信网页授权

        //得到AppID和Appsecret
        private string AppID = WebConfigurationManager.AppSettings.Get("AppID");
        private string APPSECRET = WebConfigurationManager.AppSettings.Get("AppSecret");
        protected void Page_Load(object sender, EventArgs e)
        {
            //进入页面就要进行授权,授权前和授权后访问的是同一个页面(可以修改为不是同一个界面)
            //判断是否已经进行了授权
            if (string.IsNullOrEmpty(Request.QueryString["code"]))//没有进行授权
            {
                //请求授权
                GetCode();
            }
            else
            {
                //得到了code
                string code = Request.QueryString["code"];
                TjCommon.getBugAndWriteLog("111112","得到了code为:"+ code,"");



                GetOpenID(code);
            }
        }


        public void GetCode()
        {

       
            string host = "www.baidu.com";//你需要请求的网站地址
            string path = Request.Path;//网页的路径
            string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);
            
            StringBuilder sb = new StringBuilder();
            
            sb.AppendFormat("https://open.weixin.qq.com/connect/oauth2/authorize?");
            sb.AppendFormat("appid={0}", AppID);
            sb.AppendFormat("&redirect_uri={0}", redirect_uri);//回调地址
            sb.AppendFormat("&response_type={0}", "code");
            sb.AppendFormat("&scope={0}", "snsapi_base");//只得到openid
            sb.AppendFormat("&state=123#wechat_redirect");
            try
            {
                Response.Redirect(sb.ToString());

            } catch(Exception ex)
            {
                //打印日志
                TjCommon.getBugAndWriteLog("11111",ex.ToString()+"",sb.ToString());
            }
           
        }


        public void GetOpenID(string code)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("https://api.weixin.qq.com/sns/oauth2/access_token?"); 
            sb.AppendFormat("appid={0}", AppID);
            sb.AppendFormat("&secret={0}",APPSECRET);
            sb.AppendFormat("&code={0}",code);
            sb.AppendFormat("&grant_type=authorization_code");
            lab.Text = sb.ToString();

            string relust =  senMess(sb.ToString());
            //得到返回的结果,里面保护 openid,然后得到openID后,根据业务需求进行处理。
            lab.Text += "返回结果:"+relust;


        }


        public static string senMess(string url, string postdata = "")
        {
            OHEEC.Common.Log.WriteTjLog(url);
            try
            {
                Console.WriteLine(url);
                HttpWebRequest request = null;
                request = (HttpWebRequest)WebRequest.Create(url);
                byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(postdata);
                request.Method = "POST";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;
                request.ContentType = "text/xml";
                request.Headers.Add("charset:utf-8");
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();


                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string str5 = reader.ReadToEnd();
                reader.Close();
                reader = null;
                response.Close();
                response = null;
                request.Abort();
                request = null;
                Console.WriteLine(str5);

                return str5;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());
                return ex.ToString();

            }



        }

 

你可能感兴趣的:(C#,微信开发,.net)