/// <summary>
/// 获取Access token
/// </summary>
/// <param name="appid"></param>
/// <param name="secret"></param>
/// <returns></returns>
public static string GetAccessToken()
{
//创建一个配置
var Configuration = new ConfigurationBuilder()
//添加配置文件
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
//赋值
_config = Configuration;
//读取key和secret
string appid = _config["WeiXingSession:AppID"];
string secret = _config["WeiXingSession:AppSecret"]; string strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret));
return GetJsonValue(strJson, "access_token");
} /// <summary>
/// HTTP请求功能
/// </summary>
internal class HttpRequestUtil
{
#region 请求Url,不发送数据
/// <summary>
/// 请求Url,不发送数据
/// </summary>
public static string RequestUrl(string url)
{
return RequestUrl(url, "POST");
}
#endregion #region 请求Url,不发送数据
/// <summary>
/// 请求Url,不发送数据
/// </summary>
public static string RequestUrl(string url, string method)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = method;
request.ContentType = "text/html";
request.Headers.Add("charset", "utf-8"); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}
#endregion }
public static string GetJsonValue(string jsonStr, string key)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗号,若是最后一个,截“}”号,取最小值
int end = jsonStr.IndexOf(',', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
} result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
}
}
return result;
}

ASP.NET WEBAPI 获取微信Access token的更多相关文章

  1. [原创] ASP.NET WEBAPI 接入微信公众平台 总结,Token验证失败解决办法

    首先,请允许我说一句:shit! 因为这个问题不难,但是网上有关 ASP.NET WEBAPI的资料太少.都是PHP等等的. 我也是在看了某位大神的博客后有启发,一点点研究出来的. 来看正题! 1.微 ...

  2. 微信Access Token 缓存方法

    微信Access Token默认缓存是2小时,但是需要特别强调,微信服务号和微信企业号缓存并不相同. (1)微信公众号号:每次Http请求Access Token 系统会返回不同的Token,并附带超 ...

  3. ASP.NET WebApi 基于JWT实现Token签名认证

    一.前言 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NET WebServi ...

  4. 微信公众平台开发(112) 自动更新微信access token

    关键字:Memcache access_token 更新 存储 7200 本文介绍如何存储及更新 access token的方法. 一.Access Token access_token是公众号的全局 ...

  5. 使用缓存Memcache存储更新微信access token

    关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_ ...

  6. C# v3微信 access token 过期处理的问题

    //记录access token 申请时的时间 private static DateTime GetAccessToken_Time; /// <summary> /// 过期时间为72 ...

  7. 微信access token过期

    两台服务器使用同一个微信账号(同一个app id) 时,当其中一台服务器向微信请求access token时,会造成另一台服务器的access token过期

  8. Asp.Net WebApi一个简单的Token验证

    1.前言: WebAPI主要开放数据给手机APP,Pad,其他需要得知数据的系统,或者软件应用.Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能.我上次写的<Asp.Net MV ...

  9. ASP.NET Core获取微信订单数据

    前几天对接了一波微信的订单,分享出来 1.生成签名 根据微信要求把appid.商户号.随机数.和订单号还有商户平台的密钥拼接成一个字符串然后进行MD5加密 2.拼接请求XML 然后用拼接好的XML向微 ...

  10. ASP.NET MVC获取微信返回的json数据分页

    View @model JiaYe.WeiXin.Models.ViewModels.UserViewModel <div class="pull-left pagination&qu ...

随机推荐

  1. 苹果手机备份及itunes下载更新路径

    1.itunes备份路径: C:\Users\xxx.xxx\AppData\Roaming\Apple Computer\MobileSync\Backup\ 2.itunes更新IOS路径: C: ...

  2. php微信公众号开发之生成带参数的二维码

    参考微信公众平台:https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric ...

  3. webstrom配置ES6 file watch没有生效

    参考了文章https://www.cnblogs.com/kiimi/p/8663467.html设置后,依然没有看到编译后的文件,然后设置了输出文件夹为当前项目,再重启webstrom就看到了--

  4. CF652F 题解

    题意 传送门 在一个长度为 \(m\) 的圆环上有 \(n\) 只初始位置互不相同的蚂蚁,每只蚂蚁的速度都为 \(1\),初始方向为顺时针或逆时针:两只运动方向不同的蚂蚁相遇时会调转方向,问 \(t\ ...

  5. Feign组件

    一.简介 Feign是Netflix开发的声明式,模块化的HTTP客户端 1 导入依赖 <dependency> <groupId>org.springframework.cl ...

  6. 1.1 WCF SOA架构和webservice

    1.什么是SOA? SOA全称:面向服务架构(service Oriented Architecture),它是一种组件架构模式. 一.定义 1.WebService:严格来说是行业标准,不是技术,使 ...

  7. mysql清理库中所有表的数据

    SELECT CONCAT('truncate table ',TABLE_NAME,';') AS a FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHE ...

  8. Access-Control-Allow-Origin php跨域报错

    Access-Control-Allow-Origin php跨域 解决办法: 1.PHP中echo:header(""Access-Control-Allow-Origin: * ...

  9. k8s排错(Unhealthy)

      1,组件 Unhealthy 通过kubeadm安装好kubernetes v1.18.6 查看集群状态,发现组件controller-manager 和scheduler状态 Unhealthy

  10. rpm制作(简)

    yum -y install gcc prec-devel openssl-devel zlib-devel yum -y install rpm-build #生成工作目录 rpmdev-setup ...