using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text;
using System.Web.Caching; namespace Saturn.TTS.WMS.WeiXiPortal.HelperService
{
public class WXToolsHelper
{
/// <summary>
/// 获取全局的access_token,程序缓存
/// </summary>
/// <param name="AppId">第三方用户唯一凭证</param>
/// <param name="AppSecret">第三方用户唯一凭证密钥,即appsecret</param>
/// <returns>得到的全局access_token</returns>
public string Getaccess_token(string AppId, string AppSecret)
{
try
{
//先查缓存数据
if (HttpContext.Current.Cache["access_token"] != null)
{
return HttpContext.Current.Cache["access_token"].ToString();
}
else
{
return Gettoken(AppId, AppSecret);
}
}
catch
{
return Gettoken(AppId, AppSecret);
}
} /// <summary>
/// 获取全局的access_token
/// </summary>
/// <param name="AppId">第三方用户唯一凭证</param>
/// <param name="AppSecret">第三方用户唯一凭证密钥,即appsecret</param>
/// <returns>得到的全局access_token</returns>
public string Gettoken(string AppId, string AppSecret)
{
var client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.UTF8;
var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret);
var data = client.DownloadString(url);
var jss = new JavaScriptSerializer();
var access_tokenMsg = jss.Deserialize<Dictionary<string, object>>(data);
//放入缓存中
HttpContext.Current.Cache.Insert("access_token", access_tokenMsg["access_token"], null, DateTime.Now.AddSeconds(), TimeSpan.Zero, CacheItemPriority.Normal, null); //清除jsapi_ticket缓存
HttpContext.Current.Cache.Remove("ticket"); //获取jsapi_ticket,为了同步
GetTicket(access_tokenMsg["access_token"].ToString()); return access_tokenMsg["access_token"].ToString();
} /// <summary>
/// 获取jsapi_ticket,程序缓存
/// </summary>
/// <param name="access_token">全局的access_token</param>
/// <returns>得到的jsapi_ticket</returns>
public string GetJsapi_Ticket(string access_token)
{
try
{
//先查缓存数据
if (HttpContext.Current.Cache["ticket"] != null)
{
return HttpContext.Current.Cache["ticket"].ToString();
}
else
{
return GetTicket(access_token);
}
}
catch
{
return GetTicket(access_token);
}
} /// <summary>
/// 获取jsapi_ticket
/// </summary>
/// <param name="access_token">全局的access_token</param>
/// <returns>得到的jsapi_ticket</returns>
public string GetTicket(string access_token)
{
var client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.UTF8;
var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);
var data = client.DownloadString(url);
var jss = new JavaScriptSerializer();
var ticketMsg = jss.Deserialize<Dictionary<string, object>>(data);
try
{
//放入缓存中
HttpContext.Current.Cache.Insert("ticket", ticketMsg["ticket"], null, DateTime.Now.AddSeconds(), TimeSpan.Zero, CacheItemPriority.Normal, null);
return ticketMsg["ticket"].ToString();
}
catch (Exception ex)
{
return ex.Message;
}
} /// <summary>
/// 微信权限签名的 sha1 算法
/// 签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同
/// </summary>
/// <param name="jsapi_ticket">获取到的jsapi_ticket</param>
/// <param name="noncestr">生成签名的随机串</param>
/// <param name="timestamp">生成签名的时间戳</param>
/// <param name="url">签名用的url必须是调用JS接口页面的完整URL</param>
/// <returns></returns>
public string GetShal(string jsapi_ticket, string noncestr, long timestamp, string url)
{
string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url);
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower();
} /// <summary>
/// 微信权限签名( sha1 算法 )
/// 签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同
/// </summary>
/// <param name="AppId">第三方用户唯一凭证</param>
/// /// <param name="AppSecret">第三方用户唯一凭证密钥,即appsecret</param>
/// <param name="noncestr">生成签名的随机串</param>
/// <param name="timestamp">生成签名的时间戳</param>
/// <param name="url">签名用的url必须是调用JS接口页面的完整URL</param>
/// <returns></returns>
public string signature(string AppId, string AppSecret, string noncestr, long timestamp, string url)
{
string access_token = Getaccess_token(AppId, AppSecret); //获取全局的access_token
string jsapi_ticket = GetJsapi_Ticket(access_token); //获取jsapi_ticket string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url);
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower();
} /// <summary>
/// 微信权限签名( sha1 算法 )
/// 签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同
/// </summary>
/// <param name="AppId">第三方用户唯一凭证</param>
/// /// <param name="AppSecret">第三方用户唯一凭证密钥,即appsecret</param>
/// <param name="noncestr">生成签名的随机串</param>
/// <param name="timestamp">生成签名的时间戳</param>
/// <param name="url">签名用的url必须是调用JS接口页面的完整URL</param>
/// <returns></returns>
public void signatureOut(string AppId, string AppSecret, string noncestr, long timestamp, string url, out string access_token, out string jsapi_ticket, out string signature)
{
access_token = Getaccess_token(AppId, AppSecret); //获取全局的access_token jsapi_ticket = GetJsapi_Ticket(access_token); //获取jsapi_ticket string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url); signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower();
} private string[] strs = new string[]
{
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
};
/// <summary>
/// 创建随机字符串
/// </summary>
/// <returns></returns>
public string CreatenNonce_str()
{
Random r = new Random();
var sb = new StringBuilder();
var length = strs.Length;
for (int i = ; i < ; i++)
{
sb.Append(strs[r.Next(length - )]);
}
return sb.ToString();
} /// <summary>
/// 创建时间戳
/// </summary>
/// <returns></returns>
public long CreatenTimestamp()
{
return (DateTime.Now.ToUniversalTime().Ticks - ) / ;
} }
}

微信 获取wx.config 参数 基类的更多相关文章

  1. javascript获取wx.config内部字段解决微信分享

    转自:http://www.jb51.net/article/80679.htm 专题推荐:js微信开发_脚本之家 http://www.jb51.net/Special/879.htm 背景在微信分 ...

  2. 使用javascript获取wx.config内部字段解决微信分享

    背景 在微信分享开发的时候我们通常的流程是 <?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppI ...

  3. 微信企业号 jsSDK wx.config报invalid signature错误,导致api接口无法使用

    最近在做公司定制化的时候发现一个问题,使用微信的语音API的时候微信报错,错误信息为:the permission value is offline verifying 但是诡异的是:同样的代码在我们 ...

  4. 调用微信内置的方法及wx.config的配置问题

    首先请看网址: https://www.w3cschool.cn/weixinkaifawendang/h8ap1qe5.html 重点说下怎么配置wx.config(为了安全,所有的参数都在服务端获 ...

  5. spring aspect获取抽象基类日志

    在实际的项目开发过程中我们其实封装了很多的类似BaseService.BaseDao等的基类,然后在切日志的时候我们一般是指向继承改抽象基类的实现类的,这时候我们就会出现无法切出调用抽象基类方法的日志 ...

  6. 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调

    [源码下载] 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调 作者: ...

  7. 【TP3.2.3】微信网页授权--基类

    非常好用的微信授权 基类:其他的微信权限类都可以继承至该类: <?php namespace Wechat\Controller; use Think\Controller; //微信接口基础类 ...

  8. 微信分享图标设置,以及wx.config配置

    最近公司要求我做一个关于页面分享微信显示小图和描述的功能,由于之前没有做过,所以说是从零开始,看jssdk说明文档,网上搜索各种资料,甚至连三四年前的内容都搜索出来了,也试过以前的简单方法,包括在页面 ...

  9. 微信JS-SDK接口上传图片以及wx.config的配置

    最近做的微信网页要实现一个上传图片的功能,倒腾了半天终于搞好了,具体的步骤可以查看微信官方文档https://developers.weixin.qq.com/doc/offiaccount/OA_W ...

随机推荐

  1. Tensorflow同时加载使用多个模型

    在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...

  2. PHP易混淆函数的区别及用法汇总

    本文实例分析了PHP易混淆函数的区别及用法.分享给大家供大家参考.具体分析如下: 1.echo和print的区别PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.ech ...

  3. Java -- 获取指定接口的所有实现类或获取指定类的所有继承类

    Class : ClassUtil package pri.lime.main; import java.io.File; import java.io.IOException; import jav ...

  4. CentOS安装、配置RabbitMQ

    安装步骤(rpm包安装): rpm安装官网:http://www.rabbitmq.com/install-rpm.html 下载rpm并安装: CentOs: wget http://www.rab ...

  5. 【代码审计】CLTPHP_v5.5.3 前台任意文件上传漏洞分析

      0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...

  6. PHP代码审计笔记--代码执行漏洞

    漏洞形成原因:客户端提交的参数,未经任何过滤,传入可以执行代码的函数,造成代码执行漏洞. 常见代码注射函数: 如:eval.preg_replace+/e.assert.call_user_func. ...

  7. HTML 样式

    style 属性用于改变 HTML 元素的样式,常见的样式如下: 定义字体颜色:style="color:red"定义字体大小:style="font-size:20px ...

  8. 【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟

    using UnityEngine; using System.Linq; using System.Collections.Generic; [RequireComponent(typeof(Cha ...

  9. C++ template —— tuple(十三)

    本系列博文中我们使用同类容器(如数组类型)来阐述模板的强大威力,同时,C/C++还具有包含异类对象的能力.这里的异类指的是类型不同,或者结构不同.tuple就是这样的一个类模板,它能够用于聚集不同类型 ...

  10. Java枚举根据key获取value

    package com.utcip.crm.common.constants; import com.utcip.crm.common.base.process.ScheduleStatusEnum; ...