微信jaapi签名
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}×tamp={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签名的更多相关文章
- 爬微信好友签名和QQ好友签名
先说如何爬微信好友签名,主要使用itchat,这个库提供直接的api来获取好友信息,只要用正则过滤出就行了.说一下步骤,就不贴代码了.# 登陆# 获取好友列表# 提取签名# jieba分词# word ...
- 基于weixin-java-mp 做微信JS签名 invalid signature签名错误 官方说明
微信JS签名详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang= ...
- react 项目微信端 签名失败 原因
用SPA做微信h5,调用微信jssdk的页面,安卓微信上木有问题,ios微信报当前url未注册 经过调试,是ios微信版本问题导致页面跳转url未变化,导致验签失败 所以我们大致的思想就是:在ios微 ...
- asp.net 获得伪静态网址解决微信sdk签名问题
手机网站是asp.net c#编写的,前几天因为要使用微信SDK在手机网站页面使用分享功能,但是程序使用了伪静态功能.如果原地址是:http://ww.xx.com/news/show.aspx?id ...
- 微信分享签名Java代码实现
最近写了一个小微信签名功能,记录一下希望用到的朋友可以参考下. RestController @RequestMapping("/api/wx") public class Wei ...
- 微信JSSDK签名
微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...
- 微信网页签名失败(invalid signature)
签名失败,建议按以下步骤排查 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验. 确认con ...
- 微信JS-SDK签名signature错误代码4029
一般是url的错,url需要动态获取 c#的写法: //Request.Url.ToString()这个获取的是没有端口号的有些网站有端口号 string url = Request.Url.Orig ...
- 【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】
方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; priva ...
随机推荐
- vscode 上安装 leetcode插件
vscode 真是一款非常好的轻量级的代码编辑工具,无论windows还是ubuntu使用体验都非常好. leetcode用来练练脑子吧,不要老年痴呆了. 1. 安装vscode Ubuntu先去下载 ...
- [转]Python测试框架对比----unittest, pytest, nose, robot framework对比
测试框架 什么是框架? 框架(Framework)是整个或部分系统的可重用设计,框架是用来解决代码的组织及运行控制问题的. 在我们编写自动化脚本的时候,经常需要读取配置文件,读取数据文件,发送请求 ...
- Kotlin泛型与协变及逆变原理剖析
在上一次https://www.cnblogs.com/webor2006/p/11234941.html中学习了数据类[data class]相关的知识,这次会学习关于泛型相关的东东,其中有关于泛型 ...
- springboot进行热部署项目
百度了挺多的热部署,一种就是idea中一个插件,但是听说还需要 花钱,而且效果还是不太好. 自己按照网上的经验配置了一种属于自己的热部署,下面是详细的配置过程: 一.就是引入热部署需要的依赖: < ...
- 版本管理工具:GIT
ps:项目以前一直用的svn,现在自己写代码,需要进行版本控制,学习git. 入门:推荐廖雪峰老师的git入门教程. github 上的这个项目收录了很多git教程:很全面
- keras模块学习之-激活函数(activations)--笔记
本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! 每一个神经网络层都需要一个激活函数,例如一下样例代码: from keras.layers.core import Activati ...
- Laravel下载地址合集
laravel官网直接下载地址都没了. 都要用composer装? 在此记录一下直接下载地址 master https://github.com/laravel/laravel/archiv ...
- hive函数之数学函数
hive函数之数学函数 round(double d)--返回double型d的近似值(四舍五入),返回bigint型: round(double d,int n)--返回保留double型d的n ...
- vue创建组件的方式
一.直接通过Vue.extend的方式创建组件 // 通过vue.extend 来创建全局组件 var com1 = Vue.extend({ template:'<h3>这是h3组件&l ...
- gosched
Go语言runtime.Gosched()函数浅析 这个函数的作用是让当前goroutine让出CPU,好让其它的goroutine获得执行的机会.同时,当前的goroutine也会在未来的某个时间点 ...