[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HTMLTest.Tools;
using HTMLTest.SQLConn;
using System.Collections;
namespace HTMLTest.Controllers
{
//微信接入服务器地址 直接写http://X.X.X.X/WxRequest/ServerToken_2 ;代码里面的Squall001是token
public class WxRequestController : Controller
{
//
// GET: /WxRequest/
BaseDao dao = new BaseDao();//这个是自定义的加载数据库连接类的东西,为了写log日志的,不要也没得啥子
public JsonResult ServerToken() //这个是返回json数据的,实验证明 json返回对于微信不行,所以这个方法没得用
{
if (Request.QueryString["signature"] == null ||
Request.QueryString["timestamp"] == null ||
Request.QueryString["nonce"]==null ||
Request.QueryString["echostr"]==null
)
{
return Json("Error,This Reque Not From WxServer",JsonRequestBehavior.AllowGet);
}
string signature = Request.QueryString["signature"].ToString();
string timestamp = Request.QueryString["timestamp"].ToString();
string nonce = Request.QueryString["nonce"].ToString();
string echostr = Request.QueryString["echostr"].ToString();
ArrayList paramList = new ArrayList();
paramList.Add("Squall001");
paramList.Add(timestamp);
paramList.Add(nonce);
paramList.Sort();
Dictionary<string, object> datalog = new Dictionary<string, object>();
datalog.Add("type", int.Parse("2"));
datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
datalog.Add("OpID", "0");
datalog.Add("OpLog", "sig="+signature+"&time="+timestamp+"&non="+nonce+"&echostr="+echostr);
dao.save("T_LOG", datalog);
EncryptTool MyEncrypt = new EncryptTool();
string MySignature = MyEncrypt.SHA1_Hash(paramList[0].ToString() + paramList[1].ToString() + paramList[2].ToString());
if (signature == MySignature)
{
return Json(echostr, JsonRequestBehavior.AllowGet);
}
else
{
return Json("This Reque Not From Wx",JsonRequestBehavior.AllowGet);
}
//ViewData["CallBackStr"] = "aaaaaaaaaaaaa";
//Dictionary<string, object> map = new Dictionary<string, object>();
//map.Add("appId", "aa");
//map.Add("jsapi_ticket", "bb");
//map.Add("noncestr", "cc");
//map.Add("UNIXTime", "dd");
//map.Add("signature", "ff");
////return View("ServerToken");
//string a = "abcd";
//return Json(map, JsonRequestBehavior.AllowGet);
}
public ContentResult ServerToken_2() //这个是直接返回Content的,对于微信调用,直接用这个方法即可
{
if (Request.QueryString["signature"] == null ||
Request.QueryString["timestamp"] == null ||
Request.QueryString["nonce"] == null ||
Request.QueryString["echostr"] == null
)
{
return Content("Error,This Reque Not From WxServer");
}
string signature = Request.QueryString["signature"].ToString();
string timestamp = Request.QueryString["timestamp"].ToString();
string nonce = Request.QueryString["nonce"].ToString();
string echostr = Request.QueryString["echostr"].ToString();
ArrayList paramList = new ArrayList();
paramList.Add("Squall001");
paramList.Add(timestamp);
paramList.Add(nonce);
paramList.Sort();
Dictionary<string, object> datalog = new Dictionary<string, object>();
datalog.Add("type", int.Parse("2"));
datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
datalog.Add("OpID", "0");
datalog.Add("OpLog", "sig=" + signature + "&time=" + timestamp + "&non=" + nonce + "&echostr=" + echostr);
dao.save("T_LOG", datalog);
EncryptTool MyEncrypt = new EncryptTool();
string MySignature = MyEncrypt.SHA1_Hash(paramList[0].ToString()+paramList[1].ToString()+paramList[2].ToString());
if (signature == MySignature)
{
return Content(echostr);
}
else
{
return Content("This Reque Not From Wx");
}
}
}
}
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using HTMLTest.Controllers;
using System.Web.Mvc;
namespace HTMLTest.Tools
{
public class EncryptTool
{
public string SHA1_Hash(string old_string)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_old_string = UTF8Encoding.Default.GetBytes(old_string);
byte[] bytes_new_string = sha1.ComputeHash(bytes_old_string);
string new_string = BitConverter.ToString(bytes_new_string);
new_string = new_string.Replace("-", "").ToUpper();
return new_string.ToLower();
}
}
}