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

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条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动