1. 程式人生 > >微信跳轉網頁讀取openid的方法(asp.net)

微信跳轉網頁讀取openid的方法(asp.net)

2.在微信公眾號請求使用者網頁授權之前,開發者需要先到公眾平臺官網中的“開發 - 介面許可權 - 網頁服務 - 網頁帳號 - 網頁授權獲取使用者基本資訊”的配置選項中,修改授權回撥域名。請注意,這裡填寫的是域名例如:www.qq.com,你必須配置成www.123.com

生成這個地址   https%3a%2f%2fwww.123.com%2fabc.aspx

你肯定有一個公眾號的appid=QWERTY23456

那麼你把地址改成這個

4.做sap.NET中C#的開發,abc.aspx程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.IO;
using System.Text;
using System.Net;

public partial class index002 : System.Web.UI.Page

{

    public string xxx;
    protected void Page_Load(object sender, EventArgs e)
    {
        string code = Request["code"].ToString();
        Response.Write("CODE: ");
        Response.Write(code);
        Response.Write("<br/>");
        string html = string.Empty;
        string url = "

https://api.weixin.qq.com/sns/oauth2/access_token?appid=QWERTY23456&secret=你的密碼&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();
        sr.Close();
        ioStream.Close();
        response.Close();
        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);
      
            Response.Write("OPENID: ");
            Response.Write(openid);
           

        }
        else
        {
            Response.Write("獲取openid未成功!");
        }
 

    }
}

5.ok