ajax提交时openid会丢失,所以第一次获取了,就放到cookie里,再提交时使用

JS代码

<script>$(function () {$(".submit").click(function () {var selOp = $("#selOp").val();//类型var userTEL = $("#userTEL").val();//电话var proposal = $("#proposal").val();//评价内容openidvar code = $("#hdcode").val();//var openid = $("#hdopenid").val();//if (selOp.length > 100 || proposal.length>200) {alert("输入太多字符");return false;}var url = "Opinion.aspx?act=SetOp";$.ajax({type: "post",url: url,dataType: "json",data: { selOp: selOp, userTEL: userTEL, proposal: proposal, code: code },success: function (r) {if (r.code > 0) {alert("操作成功");$(".submit").hide();$(".submit01").hide();}else {alert("操作失败");$(".submit").hide();$(".submit01").hide();}}, complete: function (r) {}});});});</script>

后台代码

using BLL;
using DAL;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace MyProject
{public partial class Opinion : System.Web.UI.Page{private string act = "";public int selOp;//= "660";//public string userTEL;//= "660";//public string proposal;//= "185";//public string submitisshow = "none";//默认隐藏提交按钮public string openid = "";////微信公众号信息部分public string appid = "appid";public string appsecret = "appsecret";public string redirect_uri = HttpUtility.UrlEncode("http://Abadfn.com/Amdfas/Opinion.aspx");//微信登录后回调地址,登录后跳转页面public string scope = "snsapi_base";
//snsapi_base: ,获取到openid
//snsapi_userinfo: 获取openid和用户资料(昵称、头像、国、省、城市、性别、权限)public string  code="";public StringBuilder sbSubjects = new StringBuilder();protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){if (Request.Cookies["openid"]==null){GetOpenid();}else{openid = Convert.ToString(Request.Cookies["openid"].Value);}if (!string.IsNullOrEmpty(Request["act"])){act = Request.QueryString["act"];}switch (act){case "SetOp":SetOp();break;default:break;}string str = "";}}/// <summary>/// 获取openid/// </summary>/// <param name="appId"></param>/// <param name="secret"></param>/// <param name="code"></param>/// <param name="grantType"></param>/// <returns></returns>public void GetOpenid(){/**微信认证获取openid部分:*临时认证code*///微信认证部分:第二步 获得codetry{code = Request["code"];if (string.IsNullOrEmpty(code)){//code = "";Response.Redirect("Opinion.html");//如果code没获取成功,重新拉取一遍//OpenAccess();//正常用这行取CODE}//微信认证部分:第三步 获得openidstring 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);HttpClient httpClient = new HttpClient();string result = HttpClientHelper.GetResponse(url);//Common.CreateWebLog("result的值", result.ToString());JObject outputObj = JObject.Parse(result);openid = outputObj["openid"].ToString();Common.CreateWebLog("获取openid成功", openid.ToString());HttpCookie objCookie = new HttpCookie("openid", openid);Response.Cookies.Add(objCookie);//return openid;}catch (Exception ex){Common.CreateWebLog("获取openid失败", ex.ToString());code = "";}}/** 接入入口* 开放到微信菜单中调用* @param $dir_url 来源url* @since 1.0* @return void*/public void OpenAccess(){//判断session不存在//if (Session["openid"] == null)//{//    //认证第一步:重定向跳转至认证网址//    string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&&response_type=code&scope={2}&m=oauth2#wechat_redirect", appid, redirect_uri, scope);//    Response.Redirect("Opinion.html");   //https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect //    Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.location.href = \""+ url + "\" </ script >");//}判断session存在//else//{//    //跳转到前端页面.aspx//    //Response.Redirect(Request.Url.ToString());//    Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.location.href = \"" + Request.Url.ToString() + "\" </ script >");//}}private void SetOp(){int code = 1;selOp = Convert.ToInt32(Request["selOp"]);userTEL = Convert.ToString(Request["userTEL"]);proposal = Convert.ToString(Request["proposal"]);OpinionModel model = new OpinionModel();model.UserID = "";model.Openid = openid;model.UserTEL = userTEL;model.TypeID = selOp;model.Proposal = proposal;//openid  userID,openid,userTEL,typeID,proposalif (proposal.Length>200|| selOp.ToString().Length>200){code = 0;}else{code=OpinionBLL.Add(model);}//code = Templates_Subjects_ResultBLL.Add02(toUserid, comeUserid, strpro);var rsp_obj = new{code = code,result = code.ToString()};Response.Write(JsonConvert.SerializeObject(rsp_obj));//将rsp_obj转化为json并输出Response.End();}}
}

中间跳转页

<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title></title><script src="JS/jquery-3.4.1.js"></script><script>$(function () {window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://adfsduan.com/Amedfdfore/Opinion.aspx&response_type=code&scope=snsapi_base&m=oauth2#wechat_redirect";});</script>
</head>
<body></body>
</html>

.net微信公众号或微信打开,静默授权,获取微信登录者的openid相关推荐

  1. 微信公众号无法抓包 提示请在微信客户端打开链接

    最近有一个需求是测试公司公众号中某个需要鉴权接口的性能.首先就是需要对该接口进行抓包,根据以前写过的一篇文章,我们可以过使用Fiddler对微信PC客户端抓包来获取接口信息. 使用fiddler抓包微 ...

  2. Python脚本--微信公众号自定义菜单的创建及获取

    暑假写的一个修改/获取微信公众号菜单的脚本,具体看微信公众平台的API:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp14452 ...

  3. 微信公众号通过第三方平台完成授权

    微信公众号通过第三方平台完成授权前,第三方平台与公众号绑定关系. 为什么要使用第三方平台来完成授权,公众号本身授权也可以,但是微信平台会认为你具有开发能力,免费提供给你的自定义菜单以及其他功能将不能再 ...

  4. Node.js七天搞定微信公众号(又名:Koa2实现电影微信公众号前后端开发)- 问题汇总

    未完待续 视频资料在上一篇博客<Node.js七天搞定微信公众号(又名:Koa2实现电影微信公众号前后端开发)> 问题1:该公众号提供的服务出现故障,请稍后重试! 出处:<第4章 实 ...

  5. 微信公众号、内嵌H5字体受微信字体大小影响问题的解决方案

    微信公众号.内嵌H5字体受微信字体大小影响问题的解决方案,代码也是之前网上找的,现在记录一下 //微信公众号禁止设置老年字体 ;(function() {if (typeof WeixinJSBrid ...

  6. php公众号提现功能,微信公众号打赏的钱怎么提现_微信公众号打赏功能_微信公众号打赏从哪里取出来...

    微信公众号打赏的钱怎么提现,微信公众号打赏功能.微信公众号之前打赏功能可以立即取现,但是最近出现了一些变动,会有人问微信公众号打赏为什么不能立刻取现?小编为大家带来了微信公众号打赏取现时间调整. 微信 ...

  7. 急急急求微信公众号开发接口-php如何实现获取用户发送的微信表情,并返回一个链接,用户点击进去链接就是刚才发的表情,用户可以保存表情?

    求微信公众号开发接口-php如何实现获取用户发送的微信表情,并返回一个链接,用户点击进去链接就是刚才发的表情,用户就可以保存表情到手机? 需求描述 1.用户在公众号对话框发送微信表情(GIF) 2.公 ...

  8. 微信小程序微信公众号支付宝小程序H5(获取信息用户信息,支付,分享,人脸识别)

    文章目录 一.微信小程序 1. 获取信息用户信息 2.支付 3.分享 4. 腾讯云小程序人脸核身 二.微信公众号 1.获取信息用户信息 2.支付 3. 分享(普通分享) 4.分享(vue单页面 配置分 ...

  9. 微信公众号页面支付接口java,[Java教程]微信公众号支付(三):页面调用微信支付JS并完成支付...

    [Java教程]微信公众号支付(三):页面调用微信支付JS并完成支付 0 2015-09-15 15:00:30 一.调用微信的JS文件 1.首先要绑定[JS接口安全域名],"公众号设置&q ...

  10. 微信公众号开发笔记(三):微信JSAPI支付功能开发

    很久之前做了微信支付,其中也趟过很多坑,现在有时间就做个自我梳理吧算是. 公众号开发的基本配置(不明白的可以参考https://blog.csdn.net/TOP__ONE/article/detai ...

最新文章

  1. python开发能做什么-python能做什么?是什么?
  2. SpringBoot中将thymeleaf升级到3.0或以上版本
  3. Leetcode:Palindrome Number
  4. 字符串匹配【模板】(luogu 3375)
  5. RMAN备份与恢复(三)--备份相关概念
  6. android半透明闪退,(Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题...
  7. struts2自定义标签_Struts 2 –没有为动作和结果输入定义结果
  8. 【笔记】shell下的主要工具
  9. vmware centos6.4挂载ntfs移动硬盘
  10. android接收红外传感器发送的脉冲信号,Arduino红外传感器-IR Receiver Module红外接收模块...
  11. Photoshop CS2 视频教程-PS色板(转)
  12. 尺规画图——三等分点
  13. BlockChain学习——Hash函数碰撞概率公式及其推导
  14. Attention UNet
  15. [OGeek2019 Final]OVM(简易虚拟机逃逸)
  16. Ubuntu 22.04 中的 .NET 6
  17. [MATLAB]从键盘输入一个带有字母构成的字符串,要求大写变小写,小写变大写输出。
  18. G、S、C、P、T STATE
  19. 【报告分享】健身内容创作者发展研究报告-Keep×新榜(附下载)
  20. 相干信号与非相干信号

热门文章

  1. SSL_read: Failure in SSL library (protocol error?)
  2. ovs-vsctl设置ofport不成功处理
  3. css中reset属性详解,css中如何使用counter-reset属性
  4. 【最优化】梯度投影法的几何意义
  5. mtk设备模型之LCM
  6. Java中RGBtoHSB_HSV/HSB颜色排序列表
  7. ArcGIS基础实验操作100例--实验13 数字化面图形的技巧
  8. Elasticsearch Java API之计数(HighLevelClient)
  9. 绿茶集团在港上市申请再失效:王勤松夫妇为实控人,翻台率不及格
  10. 区块链开发零基础必备技能之GO语言从入门到高级