Share an article about remembering the user login implementation next automatic login code in asp.net. If you need it, you can refer to it. We all use cookies for operations, the role of cookie is embodied.
// How to write the cookie and encrypt it
| The Code is as follows: |
Copy code |
Respone. Cookie. Add (new Cookie ("User_Name", (encrypted) txtUserName. text )); Public void CheckLogin () { Determine whether a Session exists first If (Session ["User_Name"] = null | Session ["User_Name"] = "") { Then determine whether there is a Cookie If (Request. Cookie ["User_Name"]! = Null & Request. Cookie ["User_Name"]! = "") { Session ["User_Name"] = (decrypted) Request. Cookie ["User_Name"]; } Else { // No Sessoin and no Cookie is forwarded to the logon page Response. ReDriect ("Login. aspx ") } } } |
The most common method of Cookies in ASP,
1. How to Write Cookies?
Response. Cookies ("field name") = variables or strings, for example:
Response. Cookies ("name2") = "Dingdang"
2. How do I set the cookie time?
Response. Cookies ("field name"). expires = Time Function + N, for example:
Response. Cookies ("name2"). expires = date + 1, indicating that Cookies are stored for one day. For example:
Response. Cookies ("name2"). expires = Hour + 8, indicating that Cookies are stored for 8 hours.
3. In the past ASP tutorials, there were few methods to exit Cookies. On the "exit" ASP page, you can write as follows:
Response. Cookies ("field name") = ""
Then, the Cookies are cleared in the client browser and the Cookies will disappear. Note how many fields should be written to clear them.
4. How to read Cookies?
Variable name = Request. Cookies ("field name"), for example:
Name2 = Request. Cookies ("name2 ")
If this sentence is written to the webpage, "Dingdang" is displayed ".
You can also directly read Cookies,
Cookies are a type of Session objects. However, Cookies do not occupy server resources, whereas Session occupies server resources. Therefore, use Cookies instead of Session.
Instance
HttpWebRequest sends a POST request to a Web server for automatic User Login
Assume that a page has the following Form ):
| The Code is as follows: |
Copy code |
<Form name = "form1" action = "http: www.breakn.com/login.asp" method = "post"> <Input type = "text" name = "userid" value = "> <Input type = "password" name = "password" value = ""> </Form>
|
From the form, we can see that the form has two form fields, one is userid and the other is password. Therefore, data submitted in the form of POST should contain these two fields.
The data format of POST is as follows:
Form domain name 1 = value 1 & form domain name 2 = value 2 & form Domain Name 3 = value 3 ......
Note that the "value" must pass through HTMLEncode, that is, it cannot contain the "<>=&" symbols.
The data to be submitted in this example should be:
| The Code is as follows: |
Copy code |
Userid = value1 & password = value2 String strId = "guest "; String strPassword = "123456 ";
ASCIIEncoding encoding = new ASCIIEncoding (); String postData = "userid =" + strId; PostData + = ("& password =" + strPassword );
Byte [] data = encoding. GetBytes (postData );
// Prepare web request... HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create ("http: www.here.com/login.asp ");
MyRequest. Method = "POST "; MyRequest. ContentType = "application/x-www-form-urlencoded "; MyRequest. ContentLength = data. Length; Stream newStream = myRequest. GetRequestStream ();
// Send the data. NewStream. Write (data, 0, data. Length ); NewStream. Close ();
// Get response HttpWebResponse myResponse = (HttpWebResponse) myRequest. GetResponse (); StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding. Default ); String content = reader. ReadToEnd (); Console. WriteLine (content ); |