微信网站登录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";
//普通授权,需要用户自己手动授权
//$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$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
最新文章
- Yocto开发笔记之《Tip-stdlib库函数strtod返回nan错误》(QQ交流群:519230208)
- DDD~领域层
- JavaScript Patterns 6.3 Klass
- Java Se :线性表
- python设计模式之--单例模式
- PHP读取excel文档
- nodetree中 前面复选框禁用插件
- 基于Live555,ffmpeg的RTSP播放器直播与点播
- sizeof的作用——解释类中与类之外static变量的情况
- Unity Twist Effect Black Hole
- 高级Bash脚本编程指南(27):文本处理命令(三)
- 阿里2016实习offer五面经验与总结(转)
- ecshop购物流程中去掉email邮箱
- Java微信公众平台开发之OAuth2.0网页授权
- Linux动态频率调节系统CPUFreq之三:governor
- FTP模式简式:PORT/PASV/EPRT/EPSV
- Oracle Drop表并未直接删除 drop table xx purge
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
- CentOS下生成密钥对(公钥、私钥)
- php判断是不是移动设备
热门文章
- 服务监控之 Spring Boot Admin.
- cocoscreator查找节点的方法 (跟jquery find一样)
- jquery 获取元素(父节点,子节点,兄弟节点)
- ubuntu16搭建文件服务器
- Java生鲜电商平台-电商虚拟币的充值与消费思考
- Format a Property Value 设置属性值的格式
- 上传图片到七牛云(客户端 js sdk)
- 1.1选择select
- [转]Workbook.SaveAs method (Excel) Password
- 北航软件学院Java历届期末考题整理