微信公众号配置类大全

微信公众号配置类大全

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web.Security;
using Newtonsoft.Json;

public class weixin
{

    public static string AppID = "*****";
    public static string AppSecret = "*****";


    /// <summary>
    /// 微信认证
    /// </summary>
    /// <param name="signature">signature</param>
    /// <param name="timestamp">timestamp</param>
    /// <param name="nonce">nonce</param>
    /// <returns></returns>
    public static bool wx_renzheng(string signature, string timestamp, string nonce)
    {
        string token = "*****";

        string[] ArrTmp = { token, timestamp, nonce };
        Array.Sort(ArrTmp);	 //字典排序
        string tmpStr = string.Join("", ArrTmp);
        tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        tmpStr = tmpStr.ToLower();
        if (tmpStr == signature)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 获取token
    /// </summary>
    /// <returns>返回token</returns>
    public static string token()
    {
        string token = "";
        //获取根目录地址
        string path = System.Web.HttpContext.Current.Request.MapPath("/");
        path = path + "App_Data\\token.txt";

        //读取token
        string old_toekn = "";
        try
        {
            string json1 = txtdata.txtread(path);
            Dictionary<string, string> list1 = JsonConvert.DeserializeObject<Dictionary<string, string>>(json1);
            //old_toekn = list1["access_token"];
            double time = double.Parse(list1["time"]);
            double now = txtdata.ConvertDateTimeInt(DateTime.Now);
            if (time > now)
            {
                old_toekn = list1["token"];
            }

        }
        catch(Exception ex) 
        {
 
        }
        if (old_toekn != "")
        {
            token = old_toekn;
        }
        else
        {
            double now = txtdata.ConvertDateTimeInt(DateTime.Now);
            double time = now + 6000;
            token = get_tk();
            Dictionary<string, string> jslist = new Dictionary<string, string>();
            jslist.Add("time",time.ToString());
            jslist.Add("token", token);
            string json2 = JsonConvert.SerializeObject(jslist);
            txtdata.txtwrite(path, json2);
        }
        return token;
    }


    /// <summary>
    /// 向服务器获取token
    /// </summary>
    /// <returns>token</returns>
    public static string get_tk()
    {
      string  url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
      string restr = "";
      try
      {
          HttpProcIP.WebClient client = new HttpProcIP.WebClient("");
          client.Encoding = System.Text.Encoding.UTF8;
          string rejson = client.OpenReadWithHttpsGET(string.Format(url, AppID,AppSecret));
          Dictionary<string, string> list1 = JsonConvert.DeserializeObject<Dictionary<string, string>>(rejson);
          restr = list1["access_token"];
      }
      catch(Exception ex)
      {
          //string estr = ex.ToString();
      }
      return restr;
    
    }
    /// <summary>
    /// 获取openid链接
    /// </summary>
    /// <returns>token</returns>
    public static string openid_url(string state="")
    {
        string url= "http://wx.lyx0916.cn/opid.cshtml";
        string href=string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect",AppID,url,state);
        return href;
    }

    /// <summary>
    /// 获取openid链接
    /// </summary>
    /// <returns>token</returns>
    public static string uid_url(string state = "")
    {
        string url = "http://wx.lyx0916.cn/opid.cshtml";
        string href = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}#wechat_redirect", AppID, url, state);
        return href;
    }
    /// <summary>
    /// 用code获取openid
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static string openid(string code)
    {
        string openid="";
        try
        {
            HttpProcIP.WebClient client= new HttpProcIP.WebClient("");
            string href=string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",AppID,AppSecret,code);
            string restr=client.OpenReadWithHttpsGET(href);
            Dictionary<string,string> dic1=JsonConvert.DeserializeObject<Dictionary<string, string>>(restr);
            openid=dic1["openid"];    
        }
        catch
        {
            
        }
        return openid;
    }
    /// <summary>
    /// 用code获取openid
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static string unid(string code)
    {
        string restr2 = "";
        try
        {
            HttpProcIP.WebClient client = new HttpProcIP.WebClient("");
            client.Encoding = System.Text.Encoding.UTF8;
            string href = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppID, AppSecret, code);
            string restr = client.OpenReadWithHttpsGET(href);
            Dictionary<string, string> dic1 = JsonConvert.DeserializeObject<Dictionary<string, string>>(restr);
            string openid = dic1["openid"];
            string token = dic1["access_token"];
            string href2 = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", token, openid);
            restr2 = client.OpenReadWithHttpsGET(href2);
        }
        catch(Exception ex)
        {
            //restr2 = ex.ToString();
        }
        return restr2;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="url"></param>
    /// <param name="openid"></param>
    /// <returns></returns>
    public static string rz_url(string url,string openid)
    {
        string token = "lixiaowei";
        string random = GenerateCheckCode(10);
        string[] ArrTmp = { token, random };
        Array.Sort(ArrTmp);	 //字典排序
        string tmpStr = string.Join("", ArrTmp);
        tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        tmpStr = tmpStr.ToLower();
        url = url + "?openid=" + openid + "&r=" + random + "&psw=" + tmpStr;
        return url;
    }




    /// <summary>
    /// 获取随机字符串
    /// </summary>
    /// <param name="codeCount"></param>
    /// <returns></returns>
    public static  string GenerateCheckCode(int codeCount)
    {
        string str = string.Empty;
        long num2 = DateTime.Now.Ticks;
        Random random = new Random((int)(((ulong)num2) & 0xffffffffL));
        for (int i = 0; i < codeCount; i++)
        {
            char ch;
            int num = random.Next();
            if ((num % 2) == 0)
            {
                ch = (char)(0x30 + ((ushort)(num % 10)));
            }
            else
            {
                ch = (char)(0x41 + ((ushort)(num % 0x1a)));
            }
            str = str + ch.ToString();
        }
        return str;
    }
    /// <summary>
    ///认证链接有效性 
    /// </summary>
    /// <param name="r"></param>
    /// <param name="psw"></param>
    /// <returns></returns>
    public static bool rz_psw(string r, string psw)
    {
        string token = "lixiaowei";
        string[] ArrTmp = { token, r };
        Array.Sort(ArrTmp);	 //字典排序
        string tmpStr = string.Join("", ArrTmp);
        tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        tmpStr = tmpStr.ToLower();
        if (psw == tmpStr)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public static string jm_href(string href,string id)
    {
        string token = "lixiaowei";
        string r = GenerateCheckCode(10);
        string[] ArrTmp = { token, r };
        Array.Sort(ArrTmp);	 //字典排序
        string tmpStr = string.Join("", ArrTmp);
        tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        tmpStr = tmpStr.ToLower();
        string re = href + string.Format("?id={0}&r={1}&psw={2}",id,r,tmpStr);
        return re;
    }








}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值