Asp.net development (1), asp.net letter Development
In the preparation work, we have completed the preparation work, and the next step is development. How should we develop it ?? Let me think about it... I think we can directly transfer user messages to our own servers !! It looks amazing.
1. first, write the verification program. The first step is to verify whether the data comes from the end. If it is not from the server end, it is easy ..., let's just talk about the code.
1.1 This is the configuration of the terminal page
1.2 verify data source
// Obtain the token
String Token = string. format ("{0}", ConfigurationManager. deleettoken ["ToKen"]); // In short, it is our own key. The key used for verification must be exactly the same as that provided by the client. config.
[HttpGet]
Public ActionResult Index (string signature, string timestamp, string nonce, string echostr) // you can learn about the related fields directly. Here we will not list them one by one.
{
String [] ArrTmp = {Token, timestamp, nonce };
Array. Sort (ArrTmp); // dictionary sorting
String tmpStr = string. Join ("", ArrTmp );
TmpStr = FormsAuthentication. HashPasswordForStoringInConfigFile (tmpStr, "SHA1"); // This is SHA1 encryption.
TmpStr = tmpStr. ToLower ();
If (signature = tmpStr) // if the incoming data is different from our data, it indicates that the incoming data is not necessarily the incoming data, and the echostr parameter cannot be returned.
{
Return Content (echostr );
}
Else
{
ReturnInfo <SessionInfo> returnSessionInfo = GetSessionUserInfo ();
If (returnSessionInfo. Code! = 1)
{
// Return the failure information to the page
Return RedirectToAction ("Index", "Error ");;
}
Return View ();
}
}
Note: Cross-Domain issues may occur during company development. We recommend that you use the ngnork tool to achieve Intranet/Internet penetration.
2. Message acquisition
2.1 message deduplication: Use msgid to remove duplicates
Static List <BaseMsg> _ queue = new List <BaseMsg> ();
// Message deduplication
If (_ queue = null)
{
_ Queue = new List <BaseMsg> ();
}
Else if (_ queue. Count> = 50)
{
_ Queue = _ queue. where (q => {return q. createTime. addSeconds (20)> DateTime. now ;}). toList (); // retain messages that have not responded within 20 seconds
}
2.2 message acquisition
Stream requestStream = System. Web. HttpContext. Current. Request. InputStream;
Byte [] requestByte = new byte [requestStream. Length];
RequestStream. Read (requestByte, 0, (int) requestStream. Length );
String requestStr = Encoding. UTF8.GetString (requestByte );
// Encapsulate the request class
XmlDocument requestDocXml = new XmlDocument ();
RequestDocXml. LoadXml (requestStr );
XmlElement rootElement = requestDocXml. DocumentElement;
XElement xdoc = XElement. Parse (requestStr );
Var msgtype = xdoc. Element ("MsgType"). Value. ToUpper ();
Var FromUserName = xdoc. Element ("FromUserName"). Value;
Var MsgId = xdoc. Element ("MsgId"). Value;
Var CreateTime = xdoc. Element ("CreateTime"). Value;
If (msgtype! = "Event" & msgtype! = "Link" & msgtype! = "Location ")
{
If (_ queue. FirstOrDefault (m => {return m. MsgFlag = MsgId;}) = null)
{
_ Queue. Add (new BaseMsg
{
CreateTime = DateTime. Now,
FromUser = FromUserName,
MsgFlag = MsgId
});
}
Else
{
Return "";
}
}
Else
{
Return "";
}
The next step is to retrieve the message of the corresponding field by parsing the rootElement.
For example, string strFormUserName = rootElement. SelectSingleNode ("FromUserName"). InnerText;
Then save the data to the database, and the entire data process for receiving and storing the message is completed.