https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
如果你是測試號,必須關注測試公衆號才能登陸授權,只有真實的服務號纔可以不用關注再授權。
Views層的頁面
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>index</title>
<script src="~/Content/js/jquery.form.js"></script>
<script>
//此頁面用於用戶點擊授權登錄,獲取CODE
/*建的MVC4項目,在默認的Home控制器下創建一個Login用於調用微信api的地址,這個Login頁面如果在微信端打開,會顯示一個是否授權的按鈕。
請注意flowerxh.cn是本人的域名,在微信公衆平臺要放置這個域名,然後Index是用來接收微信傳給我的code和state(當然需要用戶先點擊授權)。
*/
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
"appid=你自己的appID&redirect_uri=http://flowerxh.cn/Home/Index&response_type=code&" +
"scope=snsapi_userinfo&state=STATE#wechat_redirect" ;
</script>
</head>
<body>
<div>
</div>
</body>
</html>
Controllers層的控制類
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace Web.Controllers
{
public class atextController : Controller
{
//
// GET: /atext/
string _AccessTokenLogin = "";
string _OpenId = "";
public ActionResult Index()
{
return View();
}
/*
在Index控制器中接收code和state,微信的回傳地址是這樣http://flowerxh.cn/Home/Index?code=code&state=STATE
所以在Index控制器中接收這兩個值。下面將展示兩個方法
*/
//Index頁面接收微信傳來的code參數,並獲取用戶信息和配置微信簽名
[HttpGet]
public ActionResult Index(string code ,string state)
{
try
{
GetAandO(code);//用code換取access_token和openid
GetUserInfo();//獲取用戶信息
//合成簽名
//WeiXinSiagure();
return View();
}
catch (Exception)
{
throw;
}
}
/*
展示GetAandO方法,在這個方法裏,傳入剛獲取到的code值,然後拼接圖中getAO所示的地址,微信會傳來一個json對象,
其中包含accesstoken和openid等,但我們只需要這兩個,所以取出之後存入全局變量中,等待下一步調用
*/
private void GetAandO(string code)
{
var AppID = "wxxxxxxx";//你自己的appID//應用ID
var AppSecret = "cegsdfghowqklidk0266";//應用密鑰
string pageHtml = "";
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用於向Internet資源的請求進行身份驗證的網絡憑據
string getAO = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AppID + "&secret=" + AppSecret + "&code={0}&grant_type=authorization_code", code);
Byte[] pageData = MyWebClient.DownloadData(getAO);//從指定網站下載數據
MemoryStream ms = new MemoryStream(pageData);
using (StreamReader sr =new StreamReader (ms,Encoding. GetEncoding("GB2312")))
{
pageHtml = sr.ReadLine();
}
//收取accesstoken和openid
JObject jo = (JObject)JsonConvert.DeserializeObject(pageHtml) ;
_AccessTokenLogin = jo["access_token"].ToString();
_OpenId = jo["openid"].ToString();
}
/*
最後一步,實現GetUserInfo()方法,在這個方法中將獲取用戶信息,將上一步得來的accesstoken和openid傳入下面鏈接給微信,微信將返回你用戶數據,其實就是一組json對象,
* 用下列方法,取出你想要的信息,我現在取的是nickname(用戶名),用戶信息微信平臺上有,取哪個看你的需要了
*/
private void GetUserInfo()
{
string UserInfo = "";
WebClient getlast = new WebClient();
getlast.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用於向Internet資源的請求進行身份驗證的網絡憑據
string getUserInfo = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lange=zh_CN", _AccessTokenLogin, _OpenId);
Byte[] pageDataUser = getlast.DownloadData(getUserInfo);//從指定網站下載數據
MemoryStream ms2 =new MemoryStream (pageDataUser) ;
using (StreamReader sr2 = new StreamReader(ms2, Encoding.GetEncoding("utf-8")))
{
UserInfo = sr2.ReadLine();
}
JObject jo2 =(JObject) JsonConvert.DeserializeObject(UserInfo);
//string openid = jo2["openid"].ToString();
string nickname = jo2["nickname"].ToString();//微信用戶名
string headimgurl = jo2["headimgurl"].ToString();//微信用戶頭像
}
}
}