获取微信公众号openID
public class AdminUtil
{
#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 }
public class WXApi
{
#region 获取OpenId
/// <summary>
/// 获取OpenId
/// </summary>
public static string GetOpenID(string appid, string redirect_url, string code, string screct)
{
string strJson = "";
//Writelog.WritLog("进入GetOpenID方法");
//Writelog.WritLog(appid);
if (string.IsNullOrEmpty(code))
{
//Writelog.WritLog("进入if判断 true");
//Writelog.WritLog(appid);
//Writelog.WritLog(code);
redirect_url = HttpUtility.UrlEncode(redirect_url);
//这里是把路径转换为josn格式,不用转换也行 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
{
//Writelog.WritLog("进入if判断 false");
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
}
获取微信公众号openID,code
最新推荐文章于 2024-07-15 02:41:17 发布