直接上代码
namespace CloudPrj.WeiXin
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Diyi();
//string url ="https://open.weixin.qq.com/connect/qrconnect?appid=" + appid + "&redirect_uri=" + reredirect_url + "&response_type=code&scope=snsapi_login&state=" + secret + "#wechat_redirect";
}
public void Diyi()
{
string appid = "wxc14f8e4042ba5463"; //公众号appid
string secret = "322316fe831c7a2137fea0b4a62a2970"; //开发者密码
string reredirect_url = "http://www.hai121.com/WeiXin/WebForm1.aspx";
//回调链接,当请求完接口,会以get的方式传code参数给这个url
string url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appid + "&redirect_uri=" + reredirect_url + "&response_type=code&scope=snsapi_login&state=" + secret + "#wechat_redirect";
//普通授权,需要用户自己手动授权
//$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$reredirect_uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
//静默授权不需要用户手动授权,就是没有授权页面
Response.Redirect(url);
}
}
}
public partial class index_HD : Page
{
string appid = "wxc14f8e4042ba5463"; //公众号appid
string secret = "322316fe831c7a2137fea0b4a62a2970"; //开发者密码
Result t;
protected void Page_Load(object sender, EventArgs e)
{
string coem = Request["coem"];
string token = getToken(coem);
RefreshToken(token);
getuser(token);
}
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
//通过code获取openid
private string getToken(string code)
{
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
string _url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
t = JsonDeserialize<Result>(strReturn);
return t.access_token;
}
//刷新
private string RefreshToken(string token)
{
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
string _url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + token;
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
t = JsonDeserialize<Result>(strReturn);
return strReturn;
}
//获取用户
private string getuser(string token)
{
string _url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + t.access_token + "&openid=" + t.openid + "&lang=zh_CN";
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
return strReturn;
}
源码下载 https://download.csdn.net/download/weixin_41472521/10656043