一:配置信息

公众号设置:

  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的更多相关文章

  1. PHP 微信三方平台代公众号发起网页授权 获取用户信息

    1.获取code 2.通过授权回调地址的code获取用户access_token和open_id 3.通过access_token和open_id 获取用户基本信息 class wx_user { p ...

  2. 公众号H5页面接入微信登录流程

    公众号H5页面接入微信登录流程 源码地址 https://gitee.com/szxio/h5_weixin 起步 首先创建一个项目,我们采用uni-app来作为我们的前端框架 环境安装 全局安装vu ...

  3. 微信公众号h5页面自定义分享

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 微信公众号订阅号以及服务号通过网页授权获取用户openid方法

    微信官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 官方流程 网页授权流程分为四步: 1.引导用户 ...

  5. java 获取微信 页面授权 获取用户openid

    先调用微信的地址 跳转https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b4009c4fce00e0c&redirect ...

  6. 微信公众号h5页面alert去掉域名

    h5页面内嵌到微信公众号提示信息alert的时候会显示域名,去掉域名显示重写alert方法: window.alert = function(name){ var iframe = document. ...

  7. 微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。

    在这里给大家分享下我的心得,1.写代码前一定要对整个流程有个了解.我就是因为在先不了解整个过程中去ctrl+c+v他人的博客代码,花费很多无用的时间去处理还不知道能不能跑的起来的代码. 2.本人比较喜 ...

  8. 微信公众号 H5页面 支付注意细节

    1.   当秘钥(AppSecretApplets) 有问题时注意是不是已经被重置过了,此时要注意获取最新的秘钥: 2.   调试时后端的东西要放在线上https 请求 不然在手机上测试时 会被拦截: ...

  9. 20171018 在小程序页面去获取用户的OpenID

    1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...

  10. 微信公众号 H5授权登录

    首先微信公众号 必须是服务号,订阅号没有 "网页授权获取用户基本信息" 没有这个权限.服务号也必须认证后才有这个权限

随机推荐

  1. draw.io 输入数学公式

    首先我们要把数学排版功能打开: 然后输入数学公式: AsciiMath 公式由 ` 包裹,如:`a2+b2 = c^2` LaTeX 公式由 $$ 包裹,如:$$\sqrt{3×-1}+(1+x)^2 ...

  2. wget 提示 "无法验证 xxxx.xxx 的由 “xxx” 颁发的证书: 无法本地校验颁发者的权限。"

    有一天在使用 wget 下载文件时,出现了无法验证证书的提示: $ wget https://github.com/zayronxio/Mkos-Big-Sur/releases/download/0 ...

  3. Kubernetes-19:Prometheus-operator集群监控神器

    Prometheus-operator集群监控 github地址:https://github.com/prometheus-operator/kube-prometheus 具体的Prometheu ...

  4. Go语言目前主要有哪些应用框架

    Go语言是一种高效.快速.简洁的编程语言,近年来越来越受到开发者的欢迎.由于Go语言的快速发展,出现了很多的优秀框架来支持Go应用程序的开发.以下是一些目前比较流行的Go语言框架: 1. Gin:Gi ...

  5. 高维前缀和 (SOSDP)

    算法介绍--高维前缀和 引入 我们都知道二维前缀和有这么一个容斥的写法: for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ s[i][j]=s[i ...

  6. SQL 求中位值

    题目A median is defined as a number separating the higher half of a data set from the lower half. Quer ...

  7. ChatGPT介绍与使用场景

    ChatGPT是OpenAI开发的一款基于GPT-3和GPT-4的人工智能聊天机器人."GPT"代表的是"Generative Pre-trained Transform ...

  8. Yarn 3.0 Plug'n'Play (PnP) 安装和迁移

    前言 以前用 npm, 后来 yarn 火了就用 yarn. 后来 yarn 2.0 大改版, Angular 不支持就一直没用. 一直到去年的 Angular 13 才开始支持. 最近又开始写 An ...

  9. 理解IO多路复用

    I/O 多路复用是什么? I/O 多路复用是用户程序通过复用一个线程来服务多个 I/O 事件的机制,我们也可以将他说成是一个线程服务多个文件描述符 fd,而 I/O 多路复用是在操作系统层面实现提供的 ...

  10. k8s 中的 Ingress 简介

    〇.前言 前边已经介绍了 k8s 中的相关概念和 Service,本文继续看下什么是 Ingress. Ingress 的重要性不言而喻,它不仅统一了集群对外访问的入口,还提供了高级路由.七层负载均衡 ...