一:配置信息

公众号设置:

  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. Web 国际化:新增越南语语系(vue i18n)

    前提: 1. 在src/locales文件夹中,新增vi.json文件 背景: 1. vue 步骤: 1. 在main.js中, import VueI18n from 'vue-i18n' Vue. ...

  2. 深入浅出Stream流

    Java 8的新特性之一就是流stream,配合同版本出现的 Lambda ,使得操作集合(Collection)提供了极大的便利. 案例引入 在JAVA中,涉及到对数组.Collection等集合类 ...

  3. 动态规划——详解leetcode518 零钱兑换 II

    动态规划 零钱兑换 II 参考书目:<程序员代码面试指南:IT名企算法与数据结构题目最优解> 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币 ...

  4. dfs 【XR-2】奇迹——洛谷5440

    问题描述: 现有一个八位数,从左往右分别代表年月日,例如20240919,代表2024年9月19日,现将该八位数蒙住几位数,问填入数字之后有几种情况是的日为质数,月+日为质数,年+月+日为质数 输入: ...

  5. 新手指南-新人入职-maven相关

    一.前言 入职后,发现公司是用Maven对项目进行管理和构建. 一般来说,自己先确定以下几点: 1.公司对版本是否有要求. 2.是否要求IDEA对maven有特殊的配置. 3.确定自己的 MAVEN_ ...

  6. .NET 实现的交互式 OA 系统

    前言 近期,我们在后台收到了粉丝们的留言,需要一个高效办公自动化(OA)系统.为了回应大家的期待,今天我们推荐一款既灵活又强大的 OA 系统解决方案,帮助提升日常办公效率和团队协作水平. 在日常工作中 ...

  7. src 和 href 的区别?

    src:都是引用资源 src:指向外部资源的位置 , 当浏览器解析到此元素时,会暂停其它资源的下载和处理 , 直到将该资源加载 , 编译 , 执行完毕 ,相当于将资源嵌入到文档中当前元素的所在的位置: ...

  8. element+vue2下的input的样式修改

    /* 禁用下的input的字体颜色 */ /* .el-input.is-disabled /deep/ .el-input__inner { color: red; } */ /* 禁用下的inpu ...

  9. KubeSphere 使用 OpenLDAP 进行统一认证完全指南

    作者:申红磊,青云QingCloud 容器解决方案架构师,开源项目爱好者,KubeSphere Member. 背景 在实际使用中,会有一些用户,在不同场景中经常碰到 OpenLDAP 对接问题: 能 ...

  10. 使用 Cursor 和 Devbox 快速开发并上线 Gin 项目

    作为开发者,最让我们头疼的事情是什么?那必须是环境配置.版本控制以及各种部署配置等等繁琐的工作. 想象一下,如果你只需点击几下鼠标,就能拥有一个完全配置好的开发环境,支持从 Java 到 Python ...