标签:mat mem file dll byte 浅拷贝 final null title
这些代码也就开始认证的时候用一次,以后就不用了:
01.
const
string Token =
"XXXXX"
;
//你的token
02.
protected
void
Page_Load(object sender, EventArgs e)
03.
{
04.
string postStr =
""
;
05.
if
(Request.HttpMethod.ToLower() ==
"post"
)
06.
{
07.
System.IO.Stream s = System.Web.HttpContext.Current.Request.InputStream;
08.
byte
[] b =
new
byte
[s.Length];
09.
s.Read(b,
0
, (
int
)s.Length);
10.
postStr = System.Text.Encoding.UTF8.GetString(b);
11.
if
(!string.IsNullOrEmpty(postStr))
12.
{
13.
//ResponseMsg(postStr);
14.
Response.Write(ResponseMsg(postStr));
15.
Response.End();
16.
}
17.
//WriteLog("postStr:" + postStr);
18.
}
19.
else
20.
{
21.
Valid();
22.
}
23.
}
24.
25.
/// <summary>
26.
/// 验证微信签名
27.
/// </summary>
28.
/// * 将token、timestamp、nonce三个参数进行字典序排序
29.
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
30.
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
31.
/// <returns></returns>
32.
private
bool CheckSignature()
33.
{
34.
string signature = Request.QueryString[
"signature"
].ToString();
35.
string timestamp = Request.QueryString[
"timestamp"
].ToString();
36.
string nonce = Request.QueryString[
"nonce"
].ToString();
37.
string[] ArrTmp = { Token, timestamp, nonce };
38.
Array.Sort(ArrTmp);
//字典排序
39.
string tmpStr = string.Join(
""
, ArrTmp);
40.
tmpStr = FormsAuthentication.HashPass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class
=
"keylink"
>word</a>ForStoringInConfigFile(tmpStr,
"SHA1"
);
41.
tmpStr = tmpStr.ToLower();
42.
if
(tmpStr == signature)
43.
{
44.
return
true
;
45.
}
46.
else
47.
{
48.
return
false
;
49.
}
50.
}
51.
52.
53.
private
void
Valid()
54.
{
55.
string echoStr = Request.QueryString[
"echoStr"
].ToString();
56.
if
(CheckSignature())
57.
{
58.
if
(!string.IsNullOrEmpty(echoStr))
59.
{
60.
Response.Write(echoStr);
61.
Response.End();
62.
}
63.
}
64.
}
65.
66.
67.
/// <summary>
68.
/// 写日志(用于跟踪)
69.
/// </summary>
70.
private
void
WriteLog(string strMemo)
71.
{
72.
string filename = Server.MapPath(
"/logs/log.txt"
);
73.
if
(!Directory.Exists(Server.MapPath(
"//logs//"
)))
74.
Directory.CreateDirectory(
"//logs//"
);
75.
StreamWriter sr =
null
;
76.
try
77.
{
78.
if
(!File.Exists(filename))
79.
{
80.
sr = File.CreateText(filename);
81.
}
82.
else
83.
{
84.
sr = File.AppendText(filename);
85.
}
86.
sr.WriteLine(strMemo);
87.
}
88.
catch
89.
{
90.
91.
}
92.
finally
93.
{
94.
if
(sr !=
null
)
95.
sr.Close();
96.
}
97.
}
微信公共服务平台开发(.Net的实现)1 认证“成为开发者”
标签:mat mem file dll byte 浅拷贝 final null title
原文地址:http://www.cnblogs.com/wdcwy/p/6932624.html