C#實現(xiàn)的微信網頁授權操作邏輯封裝示例
本文實例講述了C#實現(xiàn)的微信網頁授權操作邏輯封裝。分享給大家供大家參考,具體如下:
一、微信網頁授權登錄
前提:
1.已經獲取的接口權限,如果是測試賬號就已經有權限了
2.配置接口的授權域名
更多說明可以參考方倍工作室:http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html
或者官網API:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
步驟:
1.用戶同意授權,獲取code
2.根據code 獲取access_token及當前操作用戶的openid、unionid
3.根據openid獲取用戶基本信息(如果需要的話)
注:如果想在網站使用掃一掃,授權登錄,可以講 _oauth.GetCodeUrl() 授權地址生成二維碼來使用
C#封裝微信網頁授權登錄使用實例:
string appid = "wx145b4a8fd07b24e8";
string appsecrect = "fe6951dcb99772411c42f724b1336065";
string redirect_url = "配置域名下的回調地址";
OAuthManage _oauth = null;
/// <summary>
///控制器構造函數
/// </summary>
public UserController()
{
_oauth = new OAuthManage(appid, appsecrect, redirect_url);
}
/// <summary>
/// 授權登錄
/// </summary>
/// <returns></returns>
public ActionResult AuthLogin()
{
ViewBag.url = _oauth.GetCodeUrl();
return View();
}
/// <summary>
/// 回調處理
/// </summary>
/// <returns></returns>
public ActionResult OAuthHandle()
{
string result = "";
//注冊事件處理
_oauth.OnError = (e) =>
{
string msg = "";
Exception inner = e;
while (inner != null)
{
msg += inner.Message;
inner = inner.InnerException;
}
result = msg;
LogOperate.Write(msg);
};
_oauth.OnGetTokenSuccess = (token) =>
{
result += "<br/>";
result += token.ToJsonString();
};
_oauth.OnGetUserInfoSuccess = (user) =>
{
result += "<br/>";
result += user.ToJsonString();
};
//第二步
_oauth.GetAccess_Token();
//第三步
_oauth.GetUserInfo();
//顯示結果
ViewBag.msg = result;
return View();
}
封裝代碼類定義:
namespace WXPackage
{
/// <summary>
/// 網頁授權邏輯處理,
/// 處理三步操作,處理成功,返回用戶基本信息
/// </summary>
public class OAuthManage
{
#region 基本信息定義
/// <summary>
/// 公眾號的唯一標識
/// </summary>
private string appid;
/// <summary>
/// 公眾號的appsecret
/// </summary>
private string secret;
/// <summary>
/// 回調url地址
/// </summary>
private string redirect_uri;
/// <summary>
/// 獲取微信用戶基本信息使用snsapi_userinfo模式
/// 如果使用靜默授權,無法獲取用戶基本信息但可以獲取到openid
/// </summary>
private string scope;
public OAuthManage(string appid, string appsecret, string redirect_uri, bool IsUserInfo = true)
{
this.appid = appid;
this.secret = appsecret;
this.redirect_uri = redirect_uri;
this.scope = IsUserInfo ? "snsapi_userinfo" : "snsapi_base";
}
#endregion
#region 請求過程信息
/// <summary>
/// 第一步獲取到的Code 值
/// </summary>
public string Code { get; set; }
/// <summary>
/// 第二步獲取到的access_token及相關數據
/// </summary>
public OAuthAccess_Token TokenData = null;
#endregion
#region 事件定義
/// <summary>
/// 當處理出現(xiàn)異常時,觸發(fā)
/// </summary>
public Action<Exception> OnError = null;
/// <summary>
/// 當獲取AccessToken成功是觸發(fā)
/// </summary>
public Action<OAuthAccess_Token> OnGetTokenSuccess = null;
/// <summary>
/// 當獲取用戶信息成功時觸發(fā)
/// </summary>
public Action<OAuthUser> OnGetUserInfoSuccess = null;
#endregion
#region 第二步,回調處理
/// <summary>
/// 第二步,通過code換取網頁授權access_token
/// </summary>
public void GetAccess_Token()
{
try
{
//1.處理跳轉
this.Code = ReqHelper.GetString("code");
if (string.IsNullOrEmpty(this.Code))
throw new Exception("獲取code參數失敗或用戶禁止授權獲取基本信息");
//1.發(fā)送獲取access_token請求
string url = GetAccess_TokenUrl();
string result = NetHelper.Get(url);
//2.解析相應結果
TokenData = JsonConvert.DeserializeObject<OAuthAccess_Token>(result);
if (TokenData == null)
throw new Exception("反序列化結果失敗,返回內容為:" + result);
//3.獲取成功
if (OnGetTokenSuccess != null)
OnGetTokenSuccess(TokenData);
}
catch (Exception ex)
{
Error("第二步,通過code換取網頁授權access_token異常", ex);
}
}
/// <summary>
/// 刷新當前access_token
/// </summary>
public OAuthAccess_Token RefreshAccess_Token()
{
try
{
//1.發(fā)送請求
string url = GetReferesh_TokenUrl();
string result = NetHelper.Get(url);
//2.解析結果
OAuthAccess_Token token = JsonConvert.DeserializeObject<OAuthAccess_Token>(result);
if (token == null)
throw new Exception("反序列化結果失敗,返回內容:" + result);
return token;
}
catch (Exception ex)
{
Error("刷新當前access_token失敗", ex);
return null;
}
}
#endregion
#region 第三步,獲取用戶基本信息
/// <summary>
/// 第三步,獲取基本信息
/// </summary>
public void GetUserInfo()
{
try
{
//1.發(fā)送get請求
string url = GetUserInfoUrl();
string result = NetHelper.Get(url);
//2.解析結果
OAuthUser user = JsonConvert.DeserializeObject<OAuthUser>(result);
if (user == null)
throw new Exception("反序列化結果失敗,返回內容:" + result);
//3.獲取成功
if (OnGetUserInfoSuccess != null)
OnGetUserInfoSuccess(user);
}
catch (Exception ex)
{
Error("第三步、獲取用戶基本信息異常", ex);
}
}
#endregion
#region 靜態(tài)方法
/// <summary>
/// 驗證授權憑證是否有效
/// </summary>
/// <param name="access_token">access_token</param>
/// <param name="openid">用戶針對當前公眾號的openid</param>
/// <returns></returns>
public static bool CheckWebAccess_Token(string access_token, string openid)
{
try
{
string url = string.Format("https://api.weixin.qq.com/sns/auth?access_token={0}&openid={1}",
access_token,
openid);
string result = NetHelper.Get(url);
JObject obj = JObject.Parse(result);
int errcode = (int)obj["errcode"];
return errcode == 0;
}
catch (Exception ex)
{
throw new Exception("," + ex.Message);
}
}
#endregion
#region 獲取請求連接
/// <summary>
/// 獲取Code的url 地址
/// </summary>
/// <returns></returns>
public string GetCodeUrl()
{
string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=STATE#wechat_redirect",
this.appid,
SecurityHelper.UrlEncode(this.redirect_uri),
this.scope);
return url;
}
/// <summary>
/// 獲取access_token的url地址
/// </summary>
/// <returns></returns>
private string GetAccess_TokenUrl()
{
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
this.appid,
this.secret,
this.Code);
return url;
}
/// <summary>
/// 獲取刷新AccessToke的地址
/// </summary>
/// <returns></returns>
private string GetReferesh_TokenUrl()
{
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}",
this.appid,
this.TokenData.refresh_token
);
return url;
}
/// <summary>
/// 獲取用戶基本信息地址
/// </summary>
/// <returns></returns>
private string GetUserInfoUrl()
{
string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN",
this.TokenData.access_token,
this.TokenData.openid);
return url;
}
#endregion
private void Error(string msg, Exception inner)
{
if (this.OnError != null)
{
this.OnError(new Exception(msg, inner));
}
}
}
/// <summary>
/// 授權之后獲取用戶基本信息
/// </summary>
public class OAuthUser
{
public string openid { get; set; }
public string nickname { get; set; }
public int sex { get; set; }
public string province { get; set; }
public string city { get; set; }
public string country { get; set; }
public string headimgurl { get; set; }
/// <summary>
/// 用戶特權信息,json 數組
/// </summary>
public JArray privilege { get; set; }
public string unionid { get; set; }
}
/// <summary>
/// 獲取Access_Token或者刷新返回的數據對象
/// </summary>
public class OAuthAccess_Token
{
public string access_token { get; set; }
public int expires_in { get; set; }
public string refresh_token { get; set; }
/// <summary>
/// 用戶針對當前公眾號的唯一標識
/// 關注后會產生,返回公眾號下頁面也會產生
/// </summary>
public string openid { get; set; }
public string scope { get; set; }
/// <summary>
/// 當前用戶的unionid,只有在用戶將公眾號綁定到微信開放平臺帳號后
/// </summary>
public string unionid { get; set; }
}
}
更多關于C#相關內容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
C#找不到類型名"SqlConnection"的有效解決方法
最近在使用c#鏈接SqlServer的時候遇到了錯誤,通過查找相關資料終于解決了,所以下面這篇文章主要給大家介紹了關于C#找不到類型名"SqlConnection"的有效解決方法,需要的朋友可以參考下2023-02-02

