本帖最后由 李维强-15级 于 2016-12-12 00:09 编辑
解决办法,在MVC里面不需要写页面出来,因为按照路由规则,实际上是访问到了control层下面某一个actionResult,然后我在这个里面把需要加密的东西写上去就行了;
整个代码如下
在control层下面新建立一个控制器,然后写如下代码
[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
{
//微信接入服务器地址 直接写[url]http://X.X.X.X/WxRequest/ServerToken_2[/url] ;代码里面的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();
}
}
} |