直接上代码

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

最新文章

  1. Yocto开发笔记之《Tip-stdlib库函数strtod返回nan错误》(QQ交流群:519230208)
  2. DDD~领域层
  3. JavaScript Patterns 6.3 Klass
  4. Java Se :线性表
  5. python设计模式之--单例模式
  6. PHP读取excel文档
  7. nodetree中 前面复选框禁用插件
  8. 基于Live555,ffmpeg的RTSP播放器直播与点播
  9. sizeof的作用——解释类中与类之外static变量的情况
  10. Unity Twist Effect Black Hole
  11. 高级Bash脚本编程指南(27):文本处理命令(三)
  12. 阿里2016实习offer五面经验与总结(转)
  13. ecshop购物流程中去掉email邮箱
  14. Java微信公众平台开发之OAuth2.0网页授权
  15. Linux动态频率调节系统CPUFreq之三:governor
  16. FTP模式简式:PORT/PASV/EPRT/EPSV
  17. Oracle Drop表并未直接删除 drop table xx purge
  18. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
  19. CentOS下生成密钥对(公钥、私钥)
  20. php判断是不是移动设备

热门文章

  1. 服务监控之 Spring Boot Admin.
  2. cocoscreator查找节点的方法 (跟jquery find一样)
  3. jquery 获取元素(父节点,子节点,兄弟节点)
  4. ubuntu16搭建文件服务器
  5. Java生鲜电商平台-电商虚拟币的充值与消费思考
  6. Format a Property Value 设置属性值的格式
  7. 上传图片到七牛云(客户端 js sdk)
  8. 1.1选择select
  9. [转]Workbook.SaveAs method (Excel) Password
  10. 北航软件学院Java历届期末考题整理