一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net记住密码实现下次自动登录代码

时间:2022-06-25 04:01:38 编辑:袖梨 来源:一聚教程网

//这里cookie怎么写,还要加密 

 代码如下 复制代码

Respone.Cookie.Add(new Cookie("User_Name",(加密)txtUserName.text)); 

public void CheckLogin()  
{  
  先判断是否有Session
  if(Session["User_Name"]==null || Session["User_Name"]=="")
  {
  再判断是否有Cookie
   if(Request.Cookie["User_Name"]!=null &&Request.Cookie["User_Name"]!="")
  {
  Session["User_Name"] = (解密)Request.Cookie["User_Name"];
  }
   else
  {
  //即没Sessoin,又没Cookie 转到登录页
  Response.ReDriect("Login.aspx")
  }
  }

Cookies在ASP中的最常用的方法,

1.如何写入Cookies?
Response.Cookies("字段名")=变量或字符串,例如:
Response.Cookies("name2")="Dingdang"

2.如何设置Cookies时间?
Response.Cookies("字段名").expires=时间函数+N,例如:
Response.Cookies("name2").expires=date+1,表示Cookies保存1天,再比如:
Response.Cookies("name2").expires=Hour+8,表示Cookies保存8小时。

3.在以往的ASP教程中,很少有介绍Cookies退出的方法。在“退出”这个ASP页中可以这样写:
Response.Cookies("字段名")=""
之后,在客户端的浏览器就清除了Cookies,并且Cookies文件会消失。注意有多少个字段,就要写多少句来清除。

4.如何读取Cookies?
变量名=Request.Cookies("字段名"),例如:
name2=Request.Cookies("name2")
如果网页中写入这句,则会显示“Dingdang”。
也可以这样直接读取Cookies,

Cookies是属于Session对象的一种。但有不同,Cookies不会占服务器资源;而“Session”则会占用服务器资源。所以,尽量不要使用Session,而使用Cookies


实例

HttpWebRequest 发送 POST 请求到一个网页服务器实现自动用户登录
假如某个页面有个如下的表单(Form):

 代码如下 复制代码
  
  
  
 

从表单可看到表单有两个表单域,一个是userid另一个是password,所以以POST形式提交的数据应该包含有这两项。
其中POST的数据格式为:
表单域名称1=值1&表单域名称2=值2&表单域名称3=值3……
要注意的是“值”必须是经过HTMLEncode的,即不能包含“<>=&”这些符号。

本例子要提交的数据应该是:

 代码如下 复制代码
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);  

 

热门栏目