这篇文章主要介绍了微信公众号开发网页中如何获取当前用户Openid,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体实现
首先,我们定一个获取openid的方法 ReGetOpenId
public static void ReGetOpenId()
{
string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//获取当前url
if (System.Web.HttpContext.Current.Session["openid"] == "" || System.Web.HttpContext.Current.Session["openid"] == null)
{
//先要判断是否是获取code后跳转过来的
if (System.Web.HttpContext.Current.Request.QueryString["code"] == "" || System.Web.HttpContext.Current.Request.QueryString["code"] == null)
{
//Code为空时,先获取Code
string GetCodeUrls = GetCodeUrl(url);
System.Web.HttpContext.Current.Response.Redirect(GetCodeUrls);//先跳转到微信的服务器,取得code后会跳回来这页面的
}
else
{
//Code非空,已经获取了code后跳回来啦,现在重新获取openid
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
string openid = "";
openid = GetOauthAccessOpenId(System.Web.HttpContext.Current.Request.QueryString["Code"]);//重新取得用户的openid
System.Web.HttpContext.Current.Session["openid"] = openid;
}
}
}
注:url最好是带域名的,花生壳的域名是行不通的,再调微信平台接口的时候,会报链接不正确错误
上文中GetCodeUrl方法如下
#region 重新获取Code的跳转链接(没有用户授权的,只能获取基本信息)
/// <summary>重新获取Code,以后面实现带着Code重新跳回目标页面(没有用户授权的,只能获取基本信息(openid))</summary>
/// <param name="url">目标页面</param>
/// <returns></returns>
public static string GetCodeUrl(string url)
{
string CodeUrl = "";
//对url进行编码
url = System.Web.HttpUtility.UrlEncode(url);
CodeUrl = string.Format("https://open.weixin.qq.com/connect/oauth3/authorize?appid=" + Appid + "&redirect_uri=" + url + "?action=viewtest&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
return CodeUrl;
}
#endregion
上文中 GetOauthAccessOpenId方法如下
#region 以Code换取用户的openid、access_token
/// <summary>根据Code获取用户的openid、access_token</summary>
public static string GetOauthAccessOpenId(string code)
{
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
string Openid = "";
string url = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=" + Appid + "&secret=" + Secret + "&code=" + code + "&grant_type=authorization_code";
string gethtml = MyHttpHelper.HttpGet(url);
log.log("拿到的url是:" + url);
log.log("获取到的gethtml是" + gethtml);
OAuth_Token ac = new OAuth_Token();
ac = JsonHelper.ToObject<OAuth_Token>(gethtml);
log.log("能否从html里拿到openid=" + ac.openid);
Openid = ac.openid;
return Openid;
}
#endregion
通过以上方法即可拿到用户的Openid,如上文所示,用户id保存在System.Web.HttpContext.Current.Session["openid"] 中,所以获取也是非常简单
在需要获取的地方执行
#region 获取当前用户Openid
ReGetOpenId();
log.log("走完获取openid的方法之后,当前Session的值是:" + System.Web.HttpContext.Current.Session["openid"]);
#endregion
注:上文中 OAuth_Token 类如下:
public class OAuth_Token
{
/// <summary>
/// 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
/// </summary>
public string access_token { get; set; }
/// <summary>
/// access_token接口调用凭证超时时间,单位(秒)
/// </summary>
public string expires_in { get; set; }
/// <summary>
/// 用户刷新access_token
/// </summary>
public string refresh_token { get; set; }
/// <summary>
/// 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
/// </summary>
public string openid { get; set; }
/// <summary>
/// 用户授权作用域
/// </summary>
public string scope { get; set; }
}
日志文件
用到的简单日志类也顺便提供放上来:
/// <summary>
/// 日志类
/// </summary>
public class Log
{
private string logFile;
private StreamWriter writer;
private FileStream fileStream = null;
public Log(string fileName)
{
logFile = fileName;
CreateDirectory(logFile);
}
public void log(string info)
{
try
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile);
if (!fileInfo.Exists)
{
fileStream = fileInfo.Create();
writer = new StreamWriter(fileStream);
}
else
{
fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write);
writer = new StreamWriter(fileStream);
}
writer.WriteLine(DateTime.Now + ": " + info);
}
finally
{
if (writer != null)
{
writer.Close();
writer.Dispose();
fileStream.Close();
fileStream.Dispose();
}
}
}
public void CreateDirectory(string infoPath)
{
DirectoryInfo directoryInfo = Directory.GetParent(infoPath);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
}
}
调用呢,很简单,调用方法如下:
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
log.log("我会被输入在日志文件中")
最后呢,拿到当前用户Openid,就可以从数据库再次获取到该用户的其他基本信息。从而可以更好的辅助你完成你项目中其他的业务模块。
感谢你能够认真阅读完这篇文章,希望小编分享的“微信公众号开发网页中如何获取当前用户Openid”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。