public WeiXinJsSignature(string weixinUrl)
{
//string url = ConfigurationManager.AppSettings["UrlAddress"] + "/Home/Index";
//string url = "http://mweb.zhijiaxing.net/circlerelationblog/Home/Index?type=11&id=5ea5b237-6ae5-49af-b7e5-64a583b1a321&ck=Zl8P3aQsTP36bskInAqlxL6p_yl9Z_emxvL8eTZsUiabYh22MoIVQw%3d%3d&from=singlemessage&isappinstalled=0";
Timestamp = GetTimeStamp();
Noncestr = Guid.NewGuid().ToString().Replace("-", "");
string jsapiTicket = GetTicket();
string sourceStr = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapiTicket, Noncestr, Timestamp, weixinUrl);
Signature = GetSHA1(sourceStr);
}

  

/// <summary>
/// 获取当前时间戳
/// </summary>
/// <returns></returns>
private string GetTimeStamp()
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return ((int)(DateTime.Now - startTime).TotalSeconds).ToString();
}

  

/// <summary>
/// SHA1加密
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private string GetSHA1(string source)
{
string rethash = "";
try
{ System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] combined = encoder.GetBytes(source);
hash.ComputeHash(combined); var sb = new StringBuilder();
foreach (var t in hash.Hash)
{
sb.Append(t.ToString("x2"));
} rethash = sb.ToString();
//rethash = Convert.ToBase64String(hash.Hash);
}
catch (Exception ex)
{
LogWriter.ToError("加密失败 : " + ex.Message);
}
return rethash;
}

  

public class HttpClientHelper
{
private static readonly HttpClient _httpClient; static HttpClientHelper()
{
_httpClient = new HttpClient();
}
public static string GetAccessToken()
{
var res = _httpClient.GetAsync(WChartConfig.Url).Result;
try
{
if (res.IsSuccessStatusCode)
{
string resCont = res.Content.ReadAsStringAsync().Result;
//{"access_token":"ACCESS_TOKEN","expires_in":7200}
if (resCont.Contains("errcode"))
{
throw new Exception(resCont);
}
else
{
AccessTokenFromWChat re = JsonConvert.DeserializeObject<AccessTokenFromWChat>(resCont);
if (re != null && !string.IsNullOrWhiteSpace(re.access_token))
{
LogWriter.ToInfo("从微信获取:AccessToken:" + re.access_token + "有效时间:" + re.expires_in);
return re.access_token;
}
}
}
else
{
throw new Exception("响应结果编码不正常");
}
}
catch (Exception ex)
{
LogWriter.ToError(ex);
}
return null;
} internal static string GetJsApiTicket(string token)
{
//{"errcode":0,"errmsg":"ok","ticket":"kgt8ON7yVITDhtdwci0qeb-gNJDxgAfi5m4ENt9ATtHvcqJU1OpSNrW4imnqjxOiXJDEvxemmRAsnclfGSPhiQ","expires_in":7200}
string url = ConfigurationLoader.JsApiTicketUrl.Replace("ACCESS_TOKEN", token);
var res = _httpClient.GetAsync(url).Result;
try
{
if (res.IsSuccessStatusCode)
{
string resCont = res.Content.ReadAsStringAsync().Result;
LogWriter.ToInfo("[GetJsApiTicket]获取到Ticket信息:" + resCont);
JsApiTicketFromWChat re = JsonConvert.DeserializeObject<JsApiTicketFromWChat>(resCont);
if (re.errcode == 0)
return re.ticket;
}
}
catch (Exception ex)
{
LogWriter.ToError(ex);
}
return null;
}
}

  

 

public class WChartConfig
{
private static readonly string GrantType = "client_credential";
public static readonly string AppId = ConfigurationManager.AppSettings.Get("AppId");
private static readonly string AppSecret = ConfigurationManager.AppSettings.Get("AppSecret");
public static readonly string Url =
string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}", GrantType, AppId, AppSecret);
}

  

public class ConfigurationLoader
{ public static readonly string JsApiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
}

  

微信jaapi签名的更多相关文章

  1. 爬微信好友签名和QQ好友签名

    先说如何爬微信好友签名,主要使用itchat,这个库提供直接的api来获取好友信息,只要用正则过滤出就行了.说一下步骤,就不贴代码了.# 登陆# 获取好友列表# 提取签名# jieba分词# word ...

  2. 基于weixin-java-mp 做微信JS签名 invalid signature签名错误 官方说明

    微信JS签名详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang= ...

  3. react 项目微信端 签名失败 原因

    用SPA做微信h5,调用微信jssdk的页面,安卓微信上木有问题,ios微信报当前url未注册 经过调试,是ios微信版本问题导致页面跳转url未变化,导致验签失败 所以我们大致的思想就是:在ios微 ...

  4. asp.net 获得伪静态网址解决微信sdk签名问题

    手机网站是asp.net c#编写的,前几天因为要使用微信SDK在手机网站页面使用分享功能,但是程序使用了伪静态功能.如果原地址是:http://ww.xx.com/news/show.aspx?id ...

  5. 微信分享签名Java代码实现

    最近写了一个小微信签名功能,记录一下希望用到的朋友可以参考下. RestController @RequestMapping("/api/wx") public class Wei ...

  6. 微信JSSDK签名

    微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...

  7. 微信网页签名失败(invalid signature)

    签名失败,建议按以下步骤排查 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验. 确认con ...

  8. 微信JS-SDK签名signature错误代码4029

    一般是url的错,url需要动态获取 c#的写法: //Request.Url.ToString()这个获取的是没有端口号的有些网站有端口号 string url = Request.Url.Orig ...

  9. 【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】

    方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; priva ...

随机推荐

  1. @RequestMapping中的注解

    在org.springframework.spring-web的jar包中在以下层级下: org.springframework.web.bind.annotation; // // Source c ...

  2. Andrew Ng机器学习 二: Logistic Regression

    一:逻辑回归(Logistic Regression) 背景:假设你是一所大学招生办的领导,你依据学生的成绩,给与他入学的资格.现在有这样一组以前的数据集ex2data1.txt,第一列表示第一次测验 ...

  3. [LeetCode] 0752. Open the Lock 打开转盘锁

    题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...

  4. selenium常用的API(一)截屏

    我们在使用selenium测试过程中,可使用截屏功能将用例执行失败的画面截图保存,方便测试执行结束后查看并定位问题. 以下介绍两种截屏方法: 对当前浏览器窗口截屏 使用selenium自带的get_s ...

  5. 51nod 2488 矩形并的面积

    在二维平面上,给定两个矩形,满足矩形的每条边分别和坐标轴平行,求这个两个矩形的并的面积.即它们重叠在一起的总的面积. 收起   输入 8个数,分别表示第一个矩形左下角坐标为(A,B),右上角坐标为(C ...

  6. InvenSense 美国公司

    InvenSense为智能型运动处理方案的先驱.全球业界的领导厂商,驱动了运动感测人机接口在消费性电子产品上的应用.公司提供的集成电路(IC)整合了运动传感器-陀螺仪以及相对应的软件,有别于其他厂商, ...

  7. 兼容火狐,Chrome,IE6,IE7,IE8的HTML换行写法

    本文链接:https://java-er.com/blog/html-break-line-firefox-chrome/ 兼容火狐,Chrome,IE6,IE7,IE8的HTML换行写法1.任意数据 ...

  8. 0.0.Pycharm使用技巧

    调整自动字体大小 Increase(字体变大) Decrease(字体变小) 背景颜色设置 pycharm 左侧菜单问题 解决 pycharm中配置启动Django项目 1.先打开mange.py,然 ...

  9. 运算符的应用及流程控制if,switch语句

    运算符的应用 1:赋值运算符    简单赋值运算符        例如var useName='tom';//简单赋值运算符    复合赋值运算符        a+=b;//相当于a=a+b;   ...

  10. web基本概念

    一.互联网 互联网(英语:Internet),又称网际网络,或音译因特网(Internet).英特网,互联网始于1969年美国的阿帕网.是网络与网络之间所串连成的庞大网络,这些网络以一组通用的协议相连 ...