c#简单获取微信openid

前端js:

点击查看代码
		var local = encodeURIComponent(window.location.href);
		var appid = $("#appId").text();//公众号
		var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + local + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
		window.location.href = url;

var appid = $("#appId").text();//公众号
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + local + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
window.location.href = url;

后端:
public async Task PayAsync(string code)
{- - -
string openid = "";
var urls3 = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appId, appSecret, code);
var httpRequestHelper = new HttpRequestHelper(urls3);
string resultTexts = await httpRequestHelper.HttpGetRequest();
AuthAccessTokenResult result1 = JsonConvert.DeserializeObject(resultTexts);
if (result1 != null)
{
openid = result1.openid;
}
}

posted @ 2023-06-08 11:28  煜灵  阅读(530)  评论(0)    收藏  举报