///? & lt summary & gt
///? Verify wechat signature
///? & lt/summary & gt;
///? *? Token, timestamp and nonce are sorted in dictionary order.
///? *? Splicing three parameter strings into one string for sha 1 encryption.
///? *? The encrypted string obtained by the developer can be compared with the signature to identify that the request originated from WeChat.
///? & ltreturns & gt& lt/returns & gt;
Private? Bull? Check the signature ()
{
String? Signature? =? Request. query string[" signature "];
String? Time stamp? =? Request. query string[" timestamp "];
String? Right now? =? Request. query string[" nonce "];
string[]? ArrTmp? =? {? Token? Time stamp,? Right now? };
Array. sort(ArrTmp); ? //Dictionary sorting
String? tmpStr? =? String. Join (",? ArrTmp);
tmpStr? =? Form authentication. hashpasswordforstoringconfigfile(tmpStr," sha 1 ");
tmpStr? =? tmpStr。 to lower();
What if? (tmpStr? ==? Signature)
{
Return? True;
}
other
{
Return? Fake;
}
}
Public? Static electricity Bull? CheckSignature (string? Signature? String? Time stamp,? String? nonce)
{
String[]? arr? =? New? String[]? {? Token? Time stamp,? Right now? };
//? Sort the three parameters token, timestamp and nonce in dictionary order?
Array. Sort & lt string & gt (arr);
StringBuilder? Content? =? New? StringBuilder();
For what? (int? Me? =? 0; ? Me? & lt? To adapt the length of by; ? i++)
{
Content. append(arr[I]);
}
String? tmpStr? =? SHA 1_Encrypt (content. ToString());
//? The encrypted string of sha 1 can be compared with signature, indicating that the request originated from WeChat?
Return? tmpStr? ! =? nulltmpStr。 Equal to (signature)? :? Fake;
}
///? & lt summary & gt
///? Encrypt the string with the default key.
///? & lt/summary & gt;
///? & ltparam? name="Source_String " >& lt/param & gt;
///? & ltreturns & gt& lt/returns & gt;
Public? Static electricity String? SHA 1_Encrypt (string? Source _ string)
{
byte[]? StrRes? =? Coding. default . GetBytes(Source _ String);
HashAlgorithm? Isa? =? New? sha 1 cryptoserviceprovider();
StrRes? =? Isa. compute hash(StrRes);
StringBuilder? EnText? =? New? StringBuilder();
foreach? (Bytes? Ebit? Are you online? StrRes)
{
EnText。 AppendFormat("{0:x2} ",? iByte);
}
Return? EnText。 ToString();
}
Private? Invalid? Effective ()
{
String? echoStr? =? Request. query string[" echoStr "];
What if? (CheckSignature())
{
What if? (! String. IsNullOrEmpty(echoStr))
{
Response. write(echoStr);
Response. end();
}
}
}
///? & lt summary & gt
///? Unix time is converted to date time.
///? & lt/summary & gt;
///? & ltparam? name = " timeStamp " & gt& lt/param & gt;
///? & ltreturns & gt& lt/returns & gt;
Private? Date and time? UnixTimeToTime (string? Time stamp)
{
Date and time? dtStart? =? Time zone. CurrentTimeZone.ToLocalTime (new? DateTime( 1970,? 1,? 1));
Dragon? lTime? =? Very long. Parse (timestamp? +? "0000000");
Time span? Tono? =? New? TimeSpan(lTime);
Return? dtStart。 Add (tonow);
}
///? & lt summary & gt
///? Convert datetime to unixtime
///? & lt/summary & gt;
///? & ltparam? name = " time " & gt& lt/param & gt;
///? & ltreturns & gt& lt/returns & gt;
Private? int? ConvertDateTimeInt(System。 Date and time? Time)
{
System. Date and time? Start time? =? Time zone. CurrentTimeZone.ToLocalTime (new? System. DateTime( 1970,? 1,? 1));
Return? (int) (time? -? Start time). Total seconds;
}
#endregion I hope you can use it.