C#-公众号H5页面授权获取用户code、openid、unionid
一:配置信息
公众号设置:
1:设置 IP白名单(所在的服务器ip)、记录公众号APPID和APPsecret;
2:设置 网页授权域名;
二:页面授权----【html中获取code】
1:页面引入js <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
2: js 跳转授权
(snsapi_userinfo指跳转页面授权!123处可随意替换随机数!)
var AppID="公众号APPID";
const { origin } = window.location //获取域名
const currentUrl = encodeURIComponent(`${origin}/HTML/Index.html?z=`)//对URL进行转码
//跳转授权
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID+ '&redirect_uri=' + currentUrl + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
//跳转授权后>会返回【currentUrl】指定页面并追加参数code! 此时仅需--获取地址参数【code】
//注意:注意:注意:currentUrl 中域名不能带端口号!!!不能带端口号!!!不能带端口号!!!【me遇到了这个问题找半天!】
//获取地址参数code
const code = GetQueryString('code')
//获取地址参数值
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]); return null;
}
三:C#--后台获取--openid、unionid (创建wx.ashx.cs)
using Common;
using HealthApp.Common.wx;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization; namespace HealthApp.Ashx
{
/// <summary>
/// wx 的摘要说明
/// </summary>
public class wx : IHttpHandler
{
public static string AppID = "";//公众号APPID
public static string AppSecret = "";//公众号AppSecret public static string GetOpenid(string code)
{
//微信认证部分:第二步 获得openid
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppID, AppSecret, code);
string result = HttpClientHelper.GetResponse(url);
JObject outputObj = JObject.Parse(result);
return outputObj["openid"].ToString(); }
public static string GetUser(string access_token, string openID)
{
//微信认证部分:第三步 获得unionid
string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", access_token, openID);
string result = HttpClientHelper.GetResponse(url); JavaScriptSerializer js = new JavaScriptSerializer();
Access_Token amodel = js.Deserialize<Access_Token>(result);//此处为定义的类,用以将json转成model
//JObject outputObj = JObject.Parse(result);
//return outputObj["unionid"].ToString();
return amodel.unionid; }
/// <summary>
/// 通过code获得access_token
/// </summary>
/// <param name="appid"></param>
/// <param name="appsecret"></param>
/// <returns></returns>
public static Access_Token Get_Access_token(string code)
{ string a_token = string.Empty;
WebClient webclient = new WebClient();
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppID, AppSecret, code);
byte[] bytes = webclient.DownloadData(url);//在指定的path上下载
string result = Encoding.GetEncoding("utf-8").GetString(bytes);//转string
//WriteTextLogHelper.WriteTextLog("获取token", result, DateTime.Now);
JavaScriptSerializer js = new JavaScriptSerializer();
Access_Token amodel = js.Deserialize<Access_Token>(result);//此处为定义的类,用以将json转成model
return amodel;
} public class Access_Token
{
public string access_token { get; set; }
public string expires_in { get; set; }
public string openid { get; set; }
public string unionid { get; set; }
public string errcode { get; set; }
}
}
}
c#后台调用示例
//传参-页面授权的code
string code = context.Request["code"].ToString();
if (string.IsNullOrEmpty(code))
{
//未授权!需要授权
//wx.OpenAccess(tj);
context.Response.Write("未授权");
}
//第二部获取access_token
var access_token = wx.Get_Access_token(code);
//第三部拉取用户信息
var unionid = wx.GetUser(access_token.access_token, access_token.openid);
var openID = access_token.openid;
检验 网页授权域名 是否通过:
(公众平台测试账号----登录公众号管理账号》输入域名进行检验)网址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

微信官方文档---授权网址 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
C#-公众号H5页面授权获取用户code、openid、unionid的更多相关文章
- PHP 微信三方平台代公众号发起网页授权 获取用户信息
1.获取code 2.通过授权回调地址的code获取用户access_token和open_id 3.通过access_token和open_id 获取用户基本信息 class wx_user { p ...
- 公众号H5页面接入微信登录流程
公众号H5页面接入微信登录流程 源码地址 https://gitee.com/szxio/h5_weixin 起步 首先创建一个项目,我们采用uni-app来作为我们的前端框架 环境安装 全局安装vu ...
- 微信公众号h5页面自定义分享
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 微信公众号订阅号以及服务号通过网页授权获取用户openid方法
微信官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 官方流程 网页授权流程分为四步: 1.引导用户 ...
- java 获取微信 页面授权 获取用户openid
先调用微信的地址 跳转https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b4009c4fce00e0c&redirect ...
- 微信公众号h5页面alert去掉域名
h5页面内嵌到微信公众号提示信息alert的时候会显示域名,去掉域名显示重写alert方法: window.alert = function(name){ var iframe = document. ...
- 微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
在这里给大家分享下我的心得,1.写代码前一定要对整个流程有个了解.我就是因为在先不了解整个过程中去ctrl+c+v他人的博客代码,花费很多无用的时间去处理还不知道能不能跑的起来的代码. 2.本人比较喜 ...
- 微信公众号 H5页面 支付注意细节
1. 当秘钥(AppSecretApplets) 有问题时注意是不是已经被重置过了,此时要注意获取最新的秘钥: 2. 调试时后端的东西要放在线上https 请求 不然在手机上测试时 会被拦截: ...
- 20171018 在小程序页面去获取用户的OpenID
1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...
- 微信公众号 H5授权登录
首先微信公众号 必须是服务号,订阅号没有 "网页授权获取用户基本信息" 没有这个权限.服务号也必须认证后才有这个权限
随机推荐
- java-GUI编程之布局类型介绍
java使用AWT和Swing相关的类可以完成图形化界面编程,其中AWT的全称是抽象窗口工具集(Abstract Window Toolkit),它是sun公司最早提供的GUI库,这个GUI库提供了一 ...
- 【转】如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?
文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和Response.Body的内容?作者: Lamond Lu地址: https://www.cnblogs.com/ ...
- C# – class, filed, property, const, readonly, get, set, init, required 使用基础
前言 心血来潮,这篇讲点基础的东西. Field 比起 Property,Field 很不起眼,你若问 JavaScript,它甚至都没有 Field. 但在 C#,class 里头真正装 value ...
- Figma 学习笔记 – Constraints 约束
用途 Constraints 用于 responsive design, 子元素和父元素建立约束关系后, 当父元素 dimension 变换的时候, 子元素会做出相应的变化 (移动位置或 resize ...
- 课时05:Linux必备系统命令
- USB总线-Linux内核USB3.0主机控制器驱动初始化流程分析(十三)
1.概述 RK3588有2个USB3.0 DRD控制器,2个USB2.0 Host控制器.USB3.0 DRD控制器既可以做Host,也可以做Device,向下兼容USB2.0和USB1.0.USB3 ...
- 5.7 函数y=Asin(ωx+φ)的图像和性质
\({\color{Red}{欢迎到学科网下载资料学习 }}\) [ [高分突破系列]高一数学上学期同步知识点剖析精品讲义与分层练习] (https://www.zxxk.com/docpack/27 ...
- 墨天轮国产数据库沙龙 | 四维纵横姚延栋 :MatrixDB,All-in-One高性能时序数据库
分享嘉宾:姚延栋 北京四维纵横数据有限公司创始人.原Greenplum 北京研发中心总经理.Greenplum中国开源社区创始人.PostgreSQL中文社区常委.壹零贰肆数字基金会(非营利组织)联合 ...
- Android复习(四)权限—>应用权限最佳做法
应用权限最佳做法 权限请求可以保护设备上的敏感信息,仅在需要访问信息以使应用正常工作时才应使用.利用本文档提供的技巧,您可能无需请求访问此类信息即可实现相同(或更好)的功能:但本文不会详细讨论权限在 ...
- 浅析Jvm
浅析Jvm 基本概念 引言 Java 虚拟机(JVM,Java Virtual Machine)是 Java 生态系统的核心组成部分,它为 Java 应用程序提供了一个运行环境.JVM 的主要职责是将 ...