public static bool checkSignature(String signature, string token, 
String timestamp, String nonce, out string 
errMsg)
        
{
            
try
            
{
                
String[] arr = new String[] { token, timestamp, nonce 
};
                
// 将token、timestamp、nonce三个参数进行字典序排序  
                
Array.Sort(arr);
                
StringBuilder content = new 
StringBuilder();
                
for (int i = 0; i < arr.Length; 
i++)
                
{
                    
content.Append(arr[i]);
                
}
                
var sha1Encryoted = 
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(content.ToString(), 
"SHA1");
                
if 
(signature.ToUpper().Equals(sha1Encryoted.ToUpper()))
                
{
                    
errMsg = 
string.Empty;
                    
return 
true;
                
}
                
else
                
{
                    
errMsg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n\r\t" + 
"签名比对失败。本地sha1为:" + 
sha1Encryoted;
                    
return 
false;
                
}
            
}
            catch 
(Exception 
e)
            
{
                
errMsg = 
e.Message;
                
return 
false;
            
}
        }
public ActionResult Index(int id) { var reqList = this.Request.QueryString; var signature = reqList["signature"]; var timestamp = reqList["timestamp"]; var nonce = reqList["nonce"]; var echostr = reqList["echostr"];
var testString = string.Format("signature:{0}, timestamp:{1}, nonce:{2}, echostr:{3}", signature, timestamp, nonce, echostr);
var wxAccount = WXAccountRepository.DetailByShopID(id); if (null == wxAccount) { WXLogRepository.WriteLog("微信管理", "验证微信帐号", id, "微信帐号不存在"); ShopRepository.UpdateStatus(false, id); return new EmptyResult(); }
var errMsg = string.Empty; bool checkResult = WeiXinConfig.checkSignature(signature, wxAccount.ToKen, timestamp, nonce, out errMsg); if (checkResult) { WXLogRepository.WriteLog("微信管理", "验证微信帐号", id, "验证微信帐号成功"); ShopRepository.UpdateStatus(true, id); return Content(echostr); }
WXLogRepository.WriteLog("微信管理", "验证微信帐号", id, "验证微信帐号失败。"); ShopRepository.UpdateStatus(false, id); return Content("fail"); }
原文:http://www.cnblogs.com/wangchengshen/p/3596023.html