旭鹏王 2017-12-06 08:42 采纳率: 100%
浏览 834
已采纳

asp.net调用微信端的JS-SDK接口,但是权限签名一直出错,步骤也没错跟微信文档的一样,急急急

///
/// 获得accesstoken
///
///
public static string AccessToken()
{
return SendRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret, Encoding.UTF8);
}

/// <summary>
/// 根据accesstoken获得ticket
/// </summary>
/// <returns></returns>
public static string GetTicket()
{
    string access_token = AccessToken();
    string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token.Substring(access_token.IndexOf(':') + 2, access_token.IndexOf(',') - 3 - access_token.IndexOf(':')) + "&type=jsapi";
    string requstStr = SendRequest(url1, Encoding.UTF8);
    string ticket = requstStr.Substring(requstStr.IndexOf("ticket") + 9, requstStr.LastIndexOf(',') - 1 - requstStr.IndexOf("ticket") - 9);// 获得json参数没搞懂的自己优化
    return ticket;
}

/// <summary>
/// 获取jssdk所需签名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetSignature(string link)
{
    DateTime dti = DateTime.Now;
    dtime = dti.ToString("yyyy-MM-dd HH:mm:ss");
    string noncestr = dti.ToString("yyyyMMddHHmmss");
    int timestamp = 1510124527;
    string ticket = GetTicket();
    time = "1510124527";
    randstr = noncestr;

    string string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + link;
    url = string1;
    string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string1, "SHA1");
    return signature.ToLower(); // 生成后一定转换为小写
}

/// <summary>   
/// Get方式获取url地址输出内容   
/// </summary> /// <param name="url">url</param>   
/// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param>   
public static string SendRequest(string url, Encoding encoding)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "GET";
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding);
    string str = sr.ReadToEnd();
    return str;
}

/// <summary>
/// 将值记录到数据库中防止出现调用限制
/// </summary>
/// <param name="url"></param>
public static void SignatureAdd(string link)
{
    time = "";
    randstr = "";
    signstr = "";
    signstr = GetSignature(link);

    string sqlI = "insert into wxinfo(timestr,nonceStr,signature,timeS,url,link) values('" + time + "','" + randstr + "','" + signstr + "','" + dtime + "','" + url + "','" + link + "') ";

    int count = DbHelper.ExecuteCommand(sqlI);
}

/// <summary>
/// 获得微信权限信息,格式:时间戳,随机数,签名
/// </summary>
/// <param name="link"></param>
/// <returns></returns>
public static string GetWXInfo(string link)
{
    bool result = false;
    // 获得最后一条新增数据
    string sql = " select * from wxinfo where link='" + link + "' order by wxinfo_id desc";
    DataTable dt = DbHelper.GetDataSet(sql);
    if (dt != null)
    {
        if (dt.Rows.Count > 0)
        {
            // 当前时间小于获得获得tincket时间时调用数据库中
            if (DateTime.Now < Convert.ToDateTime(dt.Rows[0]["timeS"].ToString()).AddSeconds(7200))
            {
                time = dt.Rows[0]["timestr"].ToString();
                randstr = dt.Rows[0]["nonceStr"].ToString();
                signstr = dt.Rows[0]["signature"].ToString();
                url = dt.Rows[0]["url"].ToString();
                result = true;
            }
        }
    }
    if (result == false)
    {
        SignatureAdd(link);
    }
    return time + "," + randstr + "," + signstr;
}

}

  • 写回答

3条回答 默认 最新

  • whyong123 2017-12-07 00:52
    关注

    建议采用第三方插件库,或者参考
    http://yzgljj.gov.cn/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 帅锅们,我很想知道这个论坛是用什么模板搭的(T_T)
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素