using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections; using System.IO; using System.Web.Services; using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Drawing; using System.Web.Script.Services; using System.Text; public partial class Comod_WXWork_GZT : System.Web.UI.Page { //public Comod_WXWork_GZT() //{ // base.Load += new EventHandler(this.Page_Load); // //this.mybutton.Click += new System.EventHandler(this.Button1_Click);//委托 // //this.Load += new System.EventHandler(this.Page_Load);//可以用AutoEventWireup属性避免的委托 //} protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HttpCookie cookie = Request.Cookies["user"];
//刷新 if (cookie!=null) { HiddenField_WXWorkId.Value = cookie.Value.ToString(); string accessToken = WXWorkHelper.GetAccessToken("wwfc0501b5df2630a9", "vqjp00QSh6SqVwWo8agNpAT5mTiT5PjMExkyR0Gz3VI"); string jsapi_ticket = WXWorkHelper.GetJsApiTicket_Application(accessToken); string jsapi_ticketE = WXWorkHelper.GetJsApiTicket_Enterprise(accessToken); //是否成功获取应用js凭证 if (jsapi_ticket.Equals("") || jsapi_ticketE.Equals("")) { } else { //获取当前的完整url string url = Request.Url.ToString(); long timestamp = SHA1Helper.CreatenTimestamp(); string nonceStr = SHA1Helper.CreatenNonce_str(); string signature = SHA1Helper.GetSignature(jsapi_ticket, nonceStr, timestamp, url); string signatureE = SHA1Helper.GetSignature(jsapi_ticketE, nonceStr, timestamp, url); HiddenField_timestamp.Value = timestamp.ToString(); HiddenField_nonceStr.Value = nonceStr; HiddenField_signature.Value = signature; HiddenField_signatureE.Value = signatureE; } } else //跳转 { ZSql getWXWorkIdSql = new ZSql();
//获取企业微信跳转链接的code var code = Request.QueryString["code"];
//传入企业id,应用secret获取AccessToken, string accessToken= WXWorkHelper.GetAccessToken("xxxx05xxxxxx", "xxxx00QShxxxxxxxxxxxxxxxxxxx"); //accessToken是否获取成功 if (accessToken.Equals("")) { //未获取到Token,提示关闭应用,重新打开,只能企业微信内打开 } else { string codeToken = "access_token=" + accessToken + "&code=" + code; string response = HttpHelper.HttpGet("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo", codeToken);
//把企业微信返回的值反序列化为对象 JObject jsonObj = (JObject)JsonConvert.DeserializeObject(response); string errcode = jsonObj["errcode"].ToString(); //是否成功从企业微信获取到userid if (errcode.Equals("0")) { string WXWorkId = jsonObj["userid"].ToString(); HiddenField_WXWorkId.Value = jsonObj["userid"].ToString(); string sql = "select StaffId,StaffName,StaffAge,StaffSex from CS_staff where wxworkID='" + WXWorkId + "'"; getWXWorkIdSql.Open(sql); //用户是否在后台存在,测试时可以不判断 if (getWXWorkIdSql.rowcount > 0) { //进入月份选择页面 HiddenField_WXWorkId.Value = WXWorkId; HiddenField_name.Value = getWXWorkIdSql.m_table.Rows[0]["staffname"].ToString().Trim(); string jsapi_ticket = WXWorkHelper.GetJsApiTicket_Application(accessToken); string jsapi_ticketE = WXWorkHelper.GetJsApiTicket_Enterprise(accessToken); //是否成功获取应用js凭证 if (jsapi_ticket.Equals("") || jsapi_ticketE.Equals("")) { } else { //获取当前的完整url string url = Request.Url.ToString(); long timestamp = SHA1Helper.CreatenTimestamp(); string nonceStr = SHA1Helper.CreatenNonce_str(); string signature = SHA1Helper.GetSignature(jsapi_ticket, nonceStr, timestamp, url); string signatureE = SHA1Helper.GetSignature(jsapi_ticketE, nonceStr, timestamp, url); HiddenField_timestamp.Value = timestamp.ToString(); HiddenField_nonceStr.Value = nonceStr; HiddenField_signature.Value = signature; HiddenField_signatureE.Value = signatureE; //避免刷新时无法获取userid HttpCookie WXWorkIdCookie = new HttpCookie("user");//定义Cookie WXWorkIdCookie.Value = WXWorkId; WXWorkIdCookie.Expires = DateTime.Now.AddHours(1);//过期时间 Response.Cookies.Add(WXWorkIdCookie); } } else { //人事表无当前用户,提示无权限,请联系人事 } } else { //未获取到Token,提示关闭应用,重新打开,只能企业微信内打开 } } } } }
翻译
搜索
复制
标签:HiddenField,string,微信,jsapi,userid,System,Value,聊天工具,using From: https://www.cnblogs.com/liangguangqing/p/17264894.html