##Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1,webkit=1,IE=edge" />
<meta name="viewport" content="user-scalable=no,width=device-width,initial-scale=1.0,maximum-scale=1.0" />
<title>微信授权登录</title>
</head>
<body>
</body>
</html>
##Default.aspx.cs
using System;
using Newtonsoft.Json;
public partial class _Default : WeiXinPage
{
protected override void OnLoad(EventArgs e)
{
Response.Write(JsonConvert.SerializeObject(Data));
base.OnLoad(e);
}
}
##WeiXinPage.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using Newtonsoft.Json;
/// <summary>
/// WeiXin 的摘要说明
/// </summary>
public abstract class WeiXinPage : Page, IRequiresSessionState
{
/// <summary>
/// 是否访客身份 true:是 false:否
/// </summary>
public static bool isGuest
{
get { return HttpContext.Current.Session[_user] == null ? true : false; }
}
private static string _user
{
get { return "$wxuser"; }
}
private static string _code
{
get { return "$wxcode"; }
}
private static string _AbsoluteUri
{
get { return HttpContext.Current.Request.Url.AbsoluteUri; }
}
/// <summary>
/// 该路径剔除微信Query的URL用于页面重定向获取最新的用户信息
/// </summary>
private static string _RawUrl
{
get
{
var url = HttpContext.Current.Request.Url.AbsoluteUri;
url = url.Replace(url.Substring(url.IndexOf("code"), url.IndexOf("STATE") + 5 - url.IndexOf("code")), "");
return url;
}
}
/// <summary>
/// 微信用户信息
/// </summary>
public Dictionary<string, object> Data
{
get { return getWxUser(); }
}
protected sealed override void OnPreInit(EventArgs e)
{
if (isGuest)
{
getWxUser();
}
base.OnPreInit(e);
}
/// <summary>
/// 获取微信用户信息
/// </summary>
private static Dictionary<string, object> getWxUser()
{
string code = HttpContext.Current.Request.QueryString.Get("code") ?? "",
url = "",
ret = "";
try
{
var user = HttpContext.Current.Session[_user];
if (code.Length == 0 && user == null)
{
JumpUrl();
return null;
}
if (user != null)
return
JsonConvert.DeserializeObject<Dictionary<string, object>>(
HttpContext.Current.Session[_user].ToString());
#region /*如果session中不存在user信息则对接微信接口获取用户信息*/
/* 根据code获取access_token */
url =
string.Format(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
, Config.AppId
, Config.AppSecret
, code);
ret = HttpService.Get(url);
var o = JsonConvert.DeserializeObject<Dictionary<string, string>>(ret);
#region 注意处理微错误{"errcode":40163, "errmsg":"code been used"} 以及{"errcode":40029, "errmsg":"invalid code"}
if (o.ContainsKey("errmsg"))
{
if (o["errcode"] == "40163" || o["errcode"] == "40029")
{
HttpContext.Current.Response.Redirect(_RawUrl, false);
return null;
}
throw new Exception(o["errmsg"]);
}
#endregion
/* 根据access_token和openid获取用户信息 */
url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN"
, o["access_token"]
, o["openid"]);
ret = HttpService.Get(url);
/*保存用户到Session*/
HttpContext.Current.Session[_user] = ret;
#endregion
return
JsonConvert.DeserializeObject<Dictionary<string, object>>(HttpContext.Current.Session[_user].ToString());
}
catch
{
throw;
}
}
/// <summary>
/// 跳转至微信授权登录页
/// </summary>
private static void JumpUrl()
{
var url =
string.Format(
"https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
, Config.AppId
, _AbsoluteUri);
HttpContext.Current.Response.Redirect(url, false);
}
}
##HttpService.cs
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
/// <summary>
/// HttpService 的摘要说明
/// </summary>
public class HttpService
{
public static string Get(string url)
{
using (var c = new WebClient())
{
try
{
c.Encoding = Encoding.UTF8;
return c.DownloadString(url);
}
catch (Exception)
{
throw;
}
}
}
public static string GetPost(string url, string parameters)
{
var ret = "";
using (var c = new WebClient())
{
try
{
c.Encoding = Encoding.UTF8;
var data = Encoding.UTF8.GetBytes(parameters);
c.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
c.Headers.Add("ContentLength", parameters.Length.ToString());
ret = Encoding.UTF8.GetString(c.UploadData(url, "POST", data));
return ret;
}
catch (Exception)
{
throw;
}
}
}
public static string GetPost(string url, NameValueCollection nv)
{
using (var c = new WebClient())
{
try
{
c.Encoding = Encoding.UTF8;
return Encoding.UTF8.GetString(c.UploadValues(url, "POST", nv));
}
catch (Exception)
{
throw;
}
}
}
}
##Config.cs
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
/// <summary>
/// 微信公众号配置文件
/// </summary>
public class Config : IRequiresSessionState
{
/// <summary>
/// 开发者ID
/// </summary>
public static string AppId { get; set; }
/// <summary>
/// 开发者密码
/// </summary>
public static string AppSecret { get; set; }
/// <summary>
/// APP access_token 7200秒更新一次
/// </summary>
public static string access_token
{
get
{
try
{
if (HttpContext.Current.Session["$access_token"] == null)
{
using (var wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
var ret =
wc.DownloadString(
string.Format(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
AppId, AppSecret));
var o = JsonConvert.DeserializeObject<Dictionary<string, object>>(ret);
if (o.ContainsKey("errmsg")) return o["errmsg"].ToString();
HttpContext.Current.Session.Add("$access_token", o["access_token"].ToString());
HttpContext.Current.Session.Timeout = 7200;
return o["access_token"].ToString();
}
}
return HttpContext.Current.Session["$access_token"].ToString();
}
catch
{
throw;
}
}
}
}
##Global.asax
<%@ Application Language="C#" %>
<script RunAt="server">
private void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Config.AppId = "wx*********aaaa";
Config.AppSecret = "5454564564646546465456";
}
private void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
private void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
private void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
}
private void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不引发该事件。
}
</script>