微信网站登录doem
直接上代码
namespace CloudPrj.WeiXin
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Diyi();
//string url ="https://open.weixin.qq.com/connect/qrconnect?appid=" + appid + "&redirect_uri=" + reredirect_url + "&response_type=code&scope=snsapi_login&state=" + secret + "#wechat_redirect";
}
public void Diyi()
{
string appid = "wxc14f8e4042ba5463"; //公众号appid
string secret = "322316fe831c7a2137fea0b4a62a2970"; //开发者密码
string reredirect_url = "http://www.hai121.com/WeiXin/WebForm1.aspx";
//回调链接,当请求完接口,会以get的方式传code参数给这个url
string url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + appid + "&redirect_uri=" + reredirect_url + "&response_type=code&scope=snsapi_login&state=" + secret + "#wechat_redirect";
//普通授权,需要用户自己手动授权
//appid&redirect_uri=$reredirect_uri&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
//静默授权不需要用户手动授权,就是没有授权页面
Response.Redirect(url);
}
}
}
public partial class index_HD : Page
{
string appid = "wxc14f8e4042ba5463"; //公众号appid
string secret = "322316fe831c7a2137fea0b4a62a2970"; //开发者密码
Result t;
protected void Page_Load(object sender, EventArgs e)
{
string coem = Request["coem"];
string token = getToken(coem);
RefreshToken(token);
getuser(token);
}
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
//通过code获取openid
private string getToken(string code)
{
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
string _url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
t = JsonDeserialize<Result>(strReturn);
return t.access_token;
}
//刷新
private string RefreshToken(string token)
{
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
string _url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + token;
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
t = JsonDeserialize<Result>(strReturn);
return strReturn;
}
//获取用户
private string getuser(string token)
{
string _url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + t.access_token + "&openid=" + t.openid + "&lang=zh_CN";
WebClient wc = new WebClient();
//取得微信返回的openid,access_token数据
String strReturn = wc.DownloadString(_url);
Result b = new Result();
//if (comHelper.InsertOp(b, "D_C_L", "D_id", this.Request, true, form1) > 0)
//{
//}
return strReturn;
}
源码下载 https://download.csdn.net/download/weixin_41472521/10656043
作者:匿名
出处:https://www.cnblogs.com/chenxi001/p/11668216.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架