一:配置信息

公众号设置:

  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. pikachu靶场-验证码

    先打开靶场,然后打开你的十米大砍刀burp,再把浏览器代理给配置好,开搞 1.先随便输入帐号和密码,用burp抓包 2.burp抓到包后用快捷键ctrl+l打开Intruder 3.确定 4.选择cl ...

  2. 真人模特失业?AI虚拟试衣一键成图,IDM-VTON下载介绍

    在电商行业竞争尤为激烈的当下,除了打价格战外,如何有效的控制成本,是每个从业者都在思考的问题 IDM-VTON是一个AI虚拟换装工具,旨在帮助服装商家解决约拍模特导致的高昂成本问题,只需一张服装图片, ...

  3. python 浅拷贝与深拷贝

    赋值引用 >>> a= {1:[1,2]}>>> b = a>>> b[2]=3>>> b {1: [1, 2], 2: 3} ...

  4. wxpython开发gui界面基础

    wxpython 开发gui 基础知识 一 .前言 记录使用wxpython开发gui工具吧.gui界面主要就是先布局,每个模块都是一个对象. 二.基础知识 import wx class MyFra ...

  5. 【基础知识】【转】彻底搞懂 async & defer

    普通 script 先来看一个普通的 script 标签. <script src="a.js"></script> 浏览器会做如下处理 停止解析 docu ...

  6. 使用Navicat Premium 将数据库导入、导出方法

    数据库导出 1.双击要导出的数据库,右键选转储SQL文件-,选择要保存的文件夹. 2.点击开始后,开始导出. 数据库导入 1.新建数据库,数据库的名字必须和导入的数据库文件一致. 2.在新建的数据库右 ...

  7. I found that CTH has no RP when i tried to reduce his RP

  8. SXYZ-6.27专题比赛

    好的,现在正式定义今天的比赛为一场伤心的比赛. ↑这张图片首先能说明一些问题,但这并不是关键. ↓这才是伤心的关键 ↑第一题文件输入输入爆 ↑第二题文件名直接爆 评语,一个比一个离谱! 然后只是很简单 ...

  9. iOS 数据持久化方案-Realm的使用小结

    一.Realm介绍 1.1.Realm是一个跨平台移动数据库引擎,支持iOS.OS X(Objective-C和Swift)以及Android,核心数据引擎C++打造,并不是建立在SQLite之上的O ...

  10. vue 中 slot 的使用方式,以及作用域插槽的用法

    分类:插槽又分为匿名插槽.具名插槽以及作用域插槽 : 匿名插槽,我们又可以叫它单个插槽或者默认插槽 因为组件标签中间是不允许写内容的,但是可以插入 插槽 :template 标签 : 插槽的使用方法 ...