C#asp.net 网站扫码登录(其实和公众号获取用户OPENID是一样的原理)

C#asp.net 网站扫码登录(其实和公众号获取用户OPENID是一样的原理)


    public void getAda()
    {




        // 在应用程序启动时运行的代码
        string AppId = "wx9cb7ef1**********";
        string AppSecret = "985f568acc4**********";

        string 绑定业务域名 = "https://www.********.com/weixinlogo.aspx";



        string getCodeStr = "https://open.weixin.qq.com/connect/qrconnect?appid=" + AppId + "&redirect_uri=" + 绑定业务域名 + "&response_type=code&scope=snsapi_login";
        string code1 = "";
        try
        {
            code1 = Context.Request.QueryString["code"].ToString();
        }
        catch
        {
            Context.Response.Redirect(getCodeStr);
        }


        string code = Context.Request.QueryString["code"].ToString();
        string html = string.Empty;
        string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AppId + "&secret=" + AppSecret + "&code=" + code + "&grant_type=authorization_code";
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream ioStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);
        html = sr.ReadToEnd();
        // TextBox1.Text = html;
        sr.Close();
        ioStream.Close();
        response.Close();


        string ot = "\"access_token\":\"";
        int startIndex0 = html.IndexOf(ot);
        if (startIndex0 != -1)
        {
            int endIndex = html.IndexOf("\",", startIndex0);
            string access_token = html.Substring(startIndex0 + ot.Length, endIndex - startIndex0 - ot.Length);


            TextBox6.Text = access_token;

        }
        else
        {
            Response.Write("找不到access_token");
        }


        string key = "\"openid\":\"";
        int startIndex = html.IndexOf(key);
        if (startIndex != -1)
        {
            int endIndex = html.IndexOf("\",", startIndex);
            string openid = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length);


            TextBox7.Text = openid;



        }
        else
        {
            Response.Write("找不到OpenidID");
        }

        WebRequest req = WebRequest.Create("https://api.weixin.qq.com/sns/userinfo?access_token=" + TextBox6.Text + "&openid=" + TextBox7.Text + "&lang=zh_CN ");  //URL
        WebResponse result = req.GetResponse();
        StreamReader sr2 = new StreamReader(result.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));   //设置编码
        string content = sr2.ReadToEnd();
        TextBox1.Text = content;


        ////  下面的 content 其实就是 上面的 TextBox1.Text 值


        string nickname = "\"nickname\":\"";
        int startIndex3 = content.IndexOf(nickname);
        if (startIndex3 != -1)
        {
            int endIndex = content.IndexOf("\",", startIndex3);


            TextBox8.Text = content.Substring(startIndex3 + nickname.Length, endIndex - startIndex3 - nickname.Length);


            // Response.Write("我的微信昵称是:" + TextBox8.Text + "

");


        }
        else
        {
            //  Response.Write("找不到nickname");
        }



        ///  city 是城市

        string city = "\"city\":\"";
        int startIndex4 = content.IndexOf(city);
        if (startIndex4 != -1)
        {
            int endIndex = content.IndexOf("\",", startIndex4);


            TextBox9.Text = content.Substring(startIndex4 + city.Length, endIndex - startIndex4 - city.Length);

            Response.Write("

" + "我的家系是:" + TextBox9.Text);
        }
        else
        {
            //  Response.Write("找不到city");
        }


        /// province 是省

        string province = "\"province\":\"";
        int startIndex5 = content.IndexOf(province);
        if (startIndex5 != -1)
        {
            int endIndex = content.IndexOf("\",", startIndex5);


            TextBox10.Text = content.Substring(startIndex5 + province.Length, endIndex - startIndex5 - province.Length);

            Response.Write("

" + "我的属于:" + TextBox10.Text);

        }
        else
        {
            // Response.Write("找不到省");
        }










        /// headimgurl  图片

        string headimgurl = "\"headimgurl\":\"";
        int startIndex6 = content.IndexOf(headimgurl);
        if (startIndex6 != -1)
        {
            int endIndex = content.IndexOf("\",", startIndex6);


            TextBox11.Text = content.Substring(startIndex6 + headimgurl.Length, endIndex - startIndex6 - headimgurl.Length);

        }
        else
        {
            //  Response.Write("找不到图片");
        }

        ////  上面得到的图片格式是http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTJicftDiaSTbbRhlmmz6pf9cwLkvlPf9TlMOkZ6SgRFD6fQ3Xwe35EbDQ9Q3209Kwg4pNw8ETFDcAbQ\/132  这样的  必须去掉 / 符号
        TextBox11.Text = TextBox11.Text.Replace("/", ""); //过滤 替换文本中包含的字符
        Image1.ImageUrl = TextBox11.Text;


      }







    protected void Page_Load(object sender, EventArgs e)
    {

        getAda();
    }
}



这样就可以了 当然这只是获取到了 微信用户的openid等个人信息,然后我们需要做的就是 查询数据库 如果存在 就不存,否则就新建用户



C#asp.net_网站扫码登录(其实和公众号获取用户OPENID是一样的原理)


是不是很简单呢?我也是新手 


遇到超时的问题,加入下面这句话 在GET数据时候 就可以了 ,

request.ServicePoint.Expect100Continue = false;


webform
桂ICP备16004754号