MVC微信網(wǎng)頁(yè)授權(quán)獲取用戶OpenId
最近開(kāi)發(fā)微信公眾平臺(tái),做下記錄,以前也開(kāi)發(fā)過(guò),這次開(kāi)發(fā)又給忘了,搞了半天,還是做個(gè)筆記為好。
注意框架為MVC 開(kāi)發(fā)微信公眾平臺(tái)。場(chǎng)景為,在模板頁(yè)中獲取用戶openid,想要進(jìn)行驗(yàn)證的頁(yè)面,集成模板頁(yè)就可以了。
在_Layout.cshtml中加入如下代碼
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@{
var code = HttpContext.Current.Request["code"];
Log.logmsg(code);
string urlpath = HttpContext.Current.Request.Url.AbsoluteUri.ToString();
ViewBag.at = AdminUtil.GetOpenID(urlpath, code);
}
</head>
類AdminUtil中加入GetOpenID方法
#region 獲取OpenID
/// <summary>
/// 獲取OpenID
/// </summary>
public static string GetOpenID(string redirect_url, string code)
{
string AppID = WXModel.AppID;
string AppSecret = WXModel.AppSecret;
string openid = "";
openid = WXApi.GetOpenID(AppID, redirect_url, code, AppSecret);
return openid;
}
#endregion
類WXApi中加入GetOpenID方法
#region 獲取OpenId
/// <summary>
/// 獲取OpenId
/// </summary>
public static string GetOpenID(string appid, string redirect_url, string code, string screct)
{
string strJson = "";
if (string.IsNullOrEmpty(code))
{
redirect_url = HttpUtility.UrlEncode(redirect_url);
HttpContext.Current.Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect",
appid, redirect_url, new Random().Next(1000, 200000).ToString()));
}
else
{
strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
appid, screct, code));
}
return Tools.GetJsonValue(strJson, "openid");
}
#endregion
public static class WXModel
{
public static string access_token;
public static string AppID;
public static string AppSecret;
}
/// <summary>
/// 工具類
/// </summary>
public class Tools
{
#region 獲取Json字符串某節(jié)點(diǎn)的值
/// <summary>
/// 獲取Json字符串某節(jié)點(diǎn)的值
/// </summary>
public static string GetJsonValue(string jsonStr, string key)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗號(hào),若是最后一個(gè),截“}”號(hào),取最小值
int end = jsonStr.IndexOf(',', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
}
result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //過(guò)濾引號(hào)或空格
}
}
return result;
}
#endregion
}
public class HttpRequestUtil
{
#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// <summary>
/// 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// </summary>
public static string RequestUrl(string url)
{
return RequestUrl(url, "POST");
}
#endregion
#region 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// <summary>
/// 請(qǐng)求Url,不發(fā)送數(shù)據(jù)
/// </summary>
public static string RequestUrl(string url, string method)
{
// 設(shè)置參數(shù)
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ContentType = "text/html";
request.Headers.Add("charset", "utf-8");
//發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.Default);
//返回結(jié)果網(wǎng)頁(yè)(html)代碼
string content = sr.ReadToEnd();
return content;
}
#endregion
}
注意:需要在微信公眾平臺(tái)中設(shè)置授權(quán)回調(diào)域

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET Core MVC學(xué)習(xí)之視圖組件(View Component)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC學(xué)習(xí)之視圖組件(View Component)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
ASP.NET Core中使用默認(rèn)MVC路由的配置
這篇文章主要介紹了ASP.NET Core中使用默認(rèn)MVC路由的配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Json日期格式問(wèn)題的四種解決方法(超詳細(xì))
這篇文章主要介紹了Json日期格式問(wèn)題的四種解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)
這篇文章主要介紹了使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理,通過(guò)引入Hangfire相關(guān)的Nuget包并對(duì)Hangfire進(jìn)行服務(wù)配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
.NET通過(guò)字典給類賦值實(shí)現(xiàn)代碼
這篇文章主要介紹了.NET通過(guò)字典給類賦值實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

