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();//微信用户头像
}
}
}