Public platform development certification "Become a developer". Net code parsing, developer. net
. Net is used to authenticate public service platform development and become a developer. The specific content is as follows:
These codes are used once for authentication and will not be used in the future:
const string Token = "XXXXX"; // your token
protected void Page_Load (object sender, EventArgs e)
{
string postStr = "";
if (Request.HttpMethod.ToLower () == "post")
{
System.IO.Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte [] b = new byte [s.Length];
s.Read (b, 0, (int) s.Length);
postStr = System.Text.Encoding.UTF8.GetString (b);
if (! string.IsNullOrEmpty (postStr))
{
// ResponseMsg (postStr);
Response.Write (ResponseMsg (postStr));
Response.End ();
}
// WriteLog ("postStr:" + postStr);
}
else
{
Valid ();
}
}
/// <summary>
/// verify signature
/// </ summary>
/// * Sort the three parameters of token, timestamp, and nonce lexicographically
/// * Splice three parameter strings into one string for sha1 encryption
/// * The encrypted string obtained by the developer can be compared with the signature to identify the source of the request.
/// <returns> </ returns>
private bool CheckSignature ()
{
string signature = Request.QueryString ["signature"]. ToString ();
string timestamp = Request.QueryString ["timestamp"]. ToString ();
string nonce = Request.QueryString ["nonce"]. ToString ();
string [] ArrTmp = {Token, timestamp, nonce};
Array.Sort (ArrTmp); // Sort by dictionary
string tmpStr = string.Join ("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile (tmpStr, "SHA1");
tmpStr = tmpStr.ToLower ();
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
.private void Valid ()
{
string echoStr = Request.QueryString ["echoStr"]. ToString ();
if (CheckSignature ())
{
if (! string.IsNullOrEmpty (echoStr))
{
Response.Write (echoStr);
Response.End ();
}
}
}
/// <summary>
/// write log (for tracking)
/// </ summary>
private void WriteLog (string strMemo)
{
string filename = Server.MapPath ("/ logs / log.txt");
if (! Directory.Exists (Server.MapPath ("// logs //")))
Directory.CreateDirectory ("// logs //");
StreamWriter sr = null;
try
{
if (! File.Exists (filename))
{
sr = File.CreateText (filename);
}
else
{
sr = File.AppendText (filename);
}
sr.WriteLine (strMemo);
}
catch
{
}
finally
{
if (sr! = null)
sr.Close ();
}
}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.