这段代码可以直接用的,主要的页面要放一个判断的 string myName= Request.Cookies["myName"].Value.Trim(); Response.Cache.SetCacheability(HttpCacheability.NoCache); if (string.IsNullOrEmpty(myName)) { Response.Redirect("~/Login.aspx"); }
——————————LOGIN.cs—————————————————— using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("myName", ""); Response.Cookies.Add(cookie); }
也可使用forms验证
location.href
{
if (Session["admin"] == null)
{
Response.Write("<script>location='Login.aspx';</script>");
}
else
{
if (string.IsNullOrEmpty(Request.QueryString["id"]))
{
DetailsView1.DefaultMode = DetailsViewMode.Insert;
}
}
}
登录界面后台CS
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("myName", "");
Response.Cookies.Add(cookie);
}
protected void btnOK_Click(object sender, EventArgs e)
{
string id = Request.Form["username"].ToString().Trim();
string pwd = Request.Form["password"].ToString().Trim();
if (BLL_Admin.Exits(id, pwd))
{
if (!BLL.BLL_Expert.exitOld(id))
{
Response.Cookies["myName"].Value = id;
Response.Redirect("Default.aspx");
}
else
{
webMesgShow("您已经提交数据,不能在登陆!");
}
}
else
{
webMesgShow("用户名或密码错误!");
}
}
string myName= Request.Cookies["myName"].Value.Trim();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (string.IsNullOrEmpty(myName))
{
Response.Redirect("~/Login.aspx");
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("myName", "");
Response.Cookies.Add(cookie);
}
protected void Button1_Click(object sender, EventArgs e)
{
string id = Request.Form["username"].ToString().Trim();
string pwd = Request.Form["password"].ToString().Trim();
if (BLL_Admin.Exits(id, pwd))
{
if (!BLL.BLL_Expert.exitOld(id))
{
Response.Cookies["myName"].Value = id;
Response.Redirect("index.aspx");
}
else
{
webMesgShow("您已经提交数据,不能在登陆!");
}
}
else
{
webMesgShow("用户名或密码错误!");
}
}}
string myName= Request.Cookies["myName"].Value.Trim();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (string.IsNullOrEmpty(myName))
{
Response.Redirect("~/Login.aspx");
}
——————————————————————————
-------------------login.aspx---------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="username" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:TextBox ID="password" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
------------------------------------------------------各位,再麻烦,帮我看看。