asp.net cookie的读写实例
程序员文章站
2024-03-09 15:24:53
asp.net操作cookie的例子,包括创建cookie、读取cookie、清空cookies,有需要的朋友可以参考下。 1、创建cookie 复制代码 代码如下: ht...
asp.net操作cookie的例子,包括创建cookie、读取cookie、清空cookies,有需要的朋友可以参考下。
1、创建cookie
httpcookie cookie = new httpcookie("autologin");
cookie.values.add("name", user.username);
cookie.values.add("pass", user.password);
cookie.expires = datetime.now.adddays(1);
response.appendcookie(cookie);
2、读取cookie
httpcookie cookies = request.cookies["autologin"];
if (!ispostback)
{
if (cookies != null)
{
string name = cookies.values["name"];
string pass = cookies.values["pass"];
if (name != null && !name.equals("") && pass != null && !pass.equals(""))
{
usermanager usermanager = new usermanager();
if (usermanager.login(name, pass, out user))
{
limits = usermanager.getlimitsbyid(user.id);
session["limits"] = limits;
session["currentuser"] = user;
response.redirect("~/display/car/carlist.aspx");
}
}
}
}
3、清空cookies
response.cookies["autologin"].expires = datetime.now.adddays(-1);
1、创建cookie
复制代码 代码如下:
httpcookie cookie = new httpcookie("autologin");
cookie.values.add("name", user.username);
cookie.values.add("pass", user.password);
cookie.expires = datetime.now.adddays(1);
response.appendcookie(cookie);
2、读取cookie
复制代码 代码如下:
httpcookie cookies = request.cookies["autologin"];
if (!ispostback)
{
if (cookies != null)
{
string name = cookies.values["name"];
string pass = cookies.values["pass"];
if (name != null && !name.equals("") && pass != null && !pass.equals(""))
{
usermanager usermanager = new usermanager();
if (usermanager.login(name, pass, out user))
{
limits = usermanager.getlimitsbyid(user.id);
session["limits"] = limits;
session["currentuser"] = user;
response.redirect("~/display/car/carlist.aspx");
}
}
}
}
3、清空cookies
复制代码 代码如下:
response.cookies["autologin"].expires = datetime.now.adddays(-1);