一、首先在某微信平臺下配置OAuth2.0受權回調頁面:數據庫
二、經過appid構造url獲取微信回傳code值(appid可在微信平臺下找到)json
1)、微信不彈出受權頁面url:api
A、code回傳到頁面wxProcess2.aspx,不帶參數數組
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
B、code回傳到頁面wxProcess2.aspx,帶參數reurl,即wxProcess2.aspx得到code的同時,也能獲取reurl的值,具體以下:微信
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
2)、微信彈出受權頁面url:須要用戶受權,才能獲取code及後面須要獲取的用戶信息微信開發
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
說明:微信是否彈出受權頁面url的區別只在一個參數scope,不彈出微信受權頁面:scope=snsapi_base,彈出微信受權頁面:scope=snsapi_userinfo。app
微信受權頁面以下:函數
三、經過appid、secret、code構造url,獲取微信用戶的openid和access token。appid、secret可在微信平臺下找到,code已在上面方法中獲取並回傳。具體訪問url:url
四、經過openid、access token獲取用戶信息,具體訪問url:spa
說明:主要經過訪問微信的3個url地址並回傳數據,獲取微信用戶基本信息
=================================================================================================================================
具體代碼:
一、獲取微信code處理頁面:wxProcess.aspx
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string reurl = "";
-
- if (Request.QueryString["reurl"] != null && Request.QueryString["reurl"] != "")
- {
- reurl = Request.QueryString["reurl"].ToString();
- }
- else
- {
- reurl = "http://www.csdn.net";
- }
- string code = "";
-
- if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
- {
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
- }
- else
- {
-
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); }
- }
- }
二、獲取微信code值回傳到本身的頁面wxProcess2.aspx:
輸出微信用戶信息: