一个很长时间没有人能解决的问题!(但不是超难)求救!!!!!!!!!!!! 当成功登陆的时候,给用户加上一个session,登陆你的每一个页面以前都先验证这个session是不是你提供给用户的,如果不是就将他踢出去,如果是就继续。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用二个web.configindex.aspx和login.aspx放在同一目录,任何人都能访问Web.config:<?xml version="1.0" encoding="utf-8" ?><configuration> <system.web> <!-- 动态调试编译 设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为 false 将提高此应用程序的运行时性能。 设置 compilation debug="true" 以将调试符号(.pdb 信息) 插入到编译页中。因为这将创建执行起来 较慢的大文件,所以应该只在调试时将该值设置为 true,而在所有其他时候都设置为 false。有关更多信息,请参考有关 调试 ASP.NET 文件的文档。 --> <compilation defaultLanguage="c#" debug="true" /> <!-- 自定义错误信息 设置 customError 模式值可以控制应向 用户显示用户友好错误信息而不是错误详细信息(包括堆栈跟踪信息): “On”始终显示自定义(友好的)信息 “Off”始终显示详细的 ASP.NET 错误信息。 “RemoteOnly”只对不在本地 Web 服务器上运行的 用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便 不向远程客户端显示应用程序的详细信息。 --> <customErrors mode="RemoteOnly" /> <!-- 身份验证 此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、 “Passport”和“None” <authentication mode="Windows"> </authentication> --> <authorization><allow users="*"/></authorization> <!-- 应用程序级别跟踪记录 应用程序级别跟踪在应用程序内为每一页启用跟踪日志输出。 设置 trace enabled="true" 以启用应用程序跟踪记录。如果 pageOutput="true",则 跟踪信息将显示在每一页的底部。否则,可以通过从 Web 应用程序 根浏览 "trace.axd" 页来查看 应用程序跟踪日志。 --> <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /> <!-- 会话状态设置 默认情况下,ASP.NET 使用 cookie 标识哪些请求属于特定的会话。 如果 cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。 若要禁用 cookie,请设置 sessionState cookieless="true"。 --> <!-- <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" /> 全球化 此节设置应用程序的全球化设置。 --> <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> </system.web></configuration>把后台管理页:mangage.aspx放入另一个目录(如manage)Web.config:<?xml version="1.0" encoding="utf-8" ?><configuration> <system.web> <!-- 动态调试编译 设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为 false 将提高此应用程序的运行时性能。 设置 compilation debug="true" 以将调试符号(.pdb 信息) 插入到编译页中。因为这将创建执行起来 较慢的大文件,所以应该只在调试时将该值设置为 true,而在所有其他时候都设置为 false。有关更多信息,请参考有关 调试 ASP.NET 文件的文档。 --> <compilation defaultLanguage="c#" debug="true" /> <!-- 自定义错误信息 设置 customError 模式值可以控制应向 用户显示用户友好错误信息而不是错误详细信息(包括堆栈跟踪信息): “On”始终显示自定义(友好的)信息 “Off”始终显示详细的 ASP.NET 错误信息。 “RemoteOnly”只对不在本地 Web 服务器上运行的 用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便 不向远程客户端显示应用程序的详细信息。 --> <customErrors mode="RemoteOnly" /> <!-- 身份验证 此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、 “Passport”和“None” --> <authentication mode="Forms"> <forms loginUrl="../longin.aspx" name=".ASPX" /> </authentication> <authorization><deny users="?"/></authorization> <!-- 应用程序级别跟踪记录 应用程序级别跟踪在应用程序内为每一页启用跟踪日志输出。 设置 trace enabled="true" 以启用应用程序跟踪记录。如果 pageOutput="true",则 跟踪信息将显示在每一页的底部。否则,可以通过从 Web 应用程序 根浏览 "trace.axd" 页来查看 应用程序跟踪日志。 --> <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /> <!-- 会话状态设置 默认情况下,ASP.NET 使用 cookie 标识哪些请求属于特定的会话。 如果 cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。 若要禁用 cookie,请设置 sessionState cookieless="true"。 --> <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" /> <!-- 全球化 此节设置应用程序的全球化设置。 --> <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312"/> </system.web></configuration> 在mangage.aspx.cs的Page_Load中添加:private void Page_Load(object sender, System.EventArgs e) { //判断session的值是否为空,如果为空,则转到login.aspx if(Session["adminName"]==null) { Response.Write("login.aspx"); Response.End(); } } 更改web.config文件: <authentication mode="Forms"> <forms name="meigui" loginUrl="login.aspx" /> </authentication> <authorization> <deny users="?"/> </authorization>在IIS添加mangage.aspx为起始页在“登陆”按钮事件中添加如下代码:if(IsValid) { string query; query = "select * from t_usermanage where username='"+ UserName.Text +"' and (role='1000' or role='1100') ";//验证有无此用户 query += "and "; query += "Password = '" + Password.Text + "'"; if (Login.userlogin(query)) { FormsAuthentication.RedirectFromLoginPage(UserName.Text ,false); Response.Redirect("mangage.aspx"); } else { Label1.Text ="用户名或密码错误!"; } } 楼上的有个地方写错了,我纠正一下!!在mangage.aspx.cs的Page_Load中添加:private void Page_Load(object sender, System.EventArgs e) { //判断session的值是否为空,如果为空,则转到login.aspx if(Session["adminName"]==null) { Response.redirect("login.aspx"); Response.End(); } } 通常做法就是用session,不管他是什么时候登录的,只要登录验证不过期就可以到manage.aspx,否则就在manage.aspx的Form_Load()里面response.redirect("index.aspx");写session:Session["User"] = "admin";判断session的方法:if(Session["User"] == null || Session["User"] != "admin"){ respose.redirect("index.aspx");} 最好的方法,在global.asax.cs里面加protected void Application_AcquireRequestState(Object sender, EventArgs E) { string[] URL = HttpContext.Current.Request.Url.Segments; string strtemp=URL[URL.Length-1]; if((strtemp!="login.aspx" && strtemp!="index.aspx" )&&(Session["UserCode"] == null|| Session["UserName"] ==null)) { Response.Redirect("login.aspx");//引导到首页或者出错页 }}你将不需要验证用户的页面放在strtemp!=后面,就可以了,其他的页面都会进行验证 不好意思,写错了:Response.Write("login.aspx");改成:Response.Redirect("login.aspx");如果你的页面在框架中,可以这样:Response.Write("<script>window.open('login.aspx','_top');</" + "script>"); cityhunter172(寒羽枫) 所讲的,Form认证比较方便 唉~~~~真是没法子,谁叫我没说清楚呢“Web.config”中的<!-- 身份验证 此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、 “Passport”和“None” --><authentication mode="Forms"> <forms loginUrl="../longin.aspx" name=".ASPX" />//其作用是保护与“Web.config”同一目录的“*.aspx”文件在没有通过验证时直接跳转至上一层目录的“login.aspx”</authentication><authorization><deny users="?"/></authorization>在“longin.aspx”的Page_Load事件里加上以下代码即可if(this.TextBox1.Text=="guest"&&this.TextBox2.Text=="123456"){ System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.username.Text,false); //将已验证身份的用户重定向回最初的页面}else{ this.Label1.ForeColor=Color.Red; this.Label1.Text="用户名不存在或密码错误。";}如果你善于观察的话,就会发现地址栏“login.aspx?ReturnUrl=…………” 不好意思,以下代码应该放在按钮事件里面if(this.TextBox1.Text=="guest"&&this.TextBox2.Text=="123456"){ System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false); //将已验证身份的用户重定向回最初的页面}else{ this.Label1.ForeColor=Color.Red; this.Label1.Text="用户名不存在或密码错误。";}还有一种方法,不用Web.config限制.此方法的作用是只能从“login.aspx”进入“mangage.aspx”在mangage.aspx的Page_Load事件里写入以下代码if(!IsPostBack){ try { login mai = (login)Context.Handler; //从“main.aspx”中取得控制权 string name=mai.TextBox1.Text; string pass=mai.TextBox2.Text; if(name=="guest"&&pass=="123456") { } else //没有通过验证 { Response.Redirect("../longin.aspx"); } } catch //不是从“main.aspx”进入此页面 { Response.Redirect("../longin.aspx"); }有时间看看我是怎么实现的:http://cityhunter.meibu.com(动态IP)上班时间开放 将图片转换成字节流 .net中单步执行没有问题,但是运行出错 IE浏览器问题!!!急!!~~~ 再问 localhost不能用,127.0.0.1 and 计算机名能用 怎么解决? DataGrid多列显示数据的问题 批量删除数据,出现超时.... this的用法?? 网站上传到服务器出错(无法找到该页...) 如何得到DataGrid控件的数据源???? Asp.Net中的公共函数调用 怎样修改datagrid超级链接列的样式? IIS @@@@@@@@@@@@@@@@ :d
index.aspx和login.aspx放在同一目录,任何人都能访问
Web.config:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<!-- 动态调试编译
设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为
false 将提高此应用程序的运行时性能。
设置 compilation debug="true" 以将调试符号(.pdb 信息)
插入到编译页中。因为这将创建执行起来
较慢的大文件,所以应该只在调试时将该值设置为 true,而在所有其他时候都设置为
false。有关更多信息,请参考有关
调试 ASP.NET 文件的文档。
-->
<compilation defaultLanguage="c#" debug="true" />
<!-- 自定义错误信息
设置 customError 模式值可以控制应向
用户显示用户友好错误信息而不是错误详细信息(包括堆栈跟踪信息): “On”始终显示自定义(友好的)信息
“Off”始终显示详细的 ASP.NET 错误信息。
“RemoteOnly”只对不在本地 Web 服务器上运行的
用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便
不向远程客户端显示应用程序的详细信息。
-->
<customErrors mode="RemoteOnly" />
<!-- 身份验证
此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、
“Passport”和“None”
<authentication mode="Windows">
</authentication> -->
<authorization><allow users="*"/></authorization>
<!-- 应用程序级别跟踪记录
应用程序级别跟踪在应用程序内为每一页启用跟踪日志输出。
设置 trace enabled="true" 以启用应用程序跟踪记录。如果 pageOutput="true",则
跟踪信息将显示在每一页的底部。否则,可以通过从 Web 应用程序
根浏览 "trace.axd" 页来查看
应用程序跟踪日志。
-->
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
<!-- 会话状态设置
默认情况下,ASP.NET 使用 cookie 标识哪些请求属于特定的会话。
如果 cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
若要禁用 cookie,请设置 sessionState cookieless="true"。
-->
<!-- <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" />
全球化
此节设置应用程序的全球化设置。
-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>
</configuration>把后台管理页:mangage.aspx放入另一个目录(如manage)Web.config:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<!-- 动态调试编译
设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为
false 将提高此应用程序的运行时性能。
设置 compilation debug="true" 以将调试符号(.pdb 信息)
插入到编译页中。因为这将创建执行起来
较慢的大文件,所以应该只在调试时将该值设置为 true,而在所有其他时候都设置为
false。有关更多信息,请参考有关
调试 ASP.NET 文件的文档。
-->
<compilation defaultLanguage="c#" debug="true" />
<!-- 自定义错误信息
设置 customError 模式值可以控制应向
用户显示用户友好错误信息而不是错误详细信息(包括堆栈跟踪信息): “On”始终显示自定义(友好的)信息
“Off”始终显示详细的 ASP.NET 错误信息。
“RemoteOnly”只对不在本地 Web 服务器上运行的
用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便
不向远程客户端显示应用程序的详细信息。
-->
<customErrors mode="RemoteOnly" />
<!-- 身份验证
此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、
“Passport”和“None”
-->
<authentication mode="Forms">
<forms loginUrl="../longin.aspx" name=".ASPX" />
</authentication>
<authorization><deny users="?"/></authorization>
<!-- 应用程序级别跟踪记录
应用程序级别跟踪在应用程序内为每一页启用跟踪日志输出。
设置 trace enabled="true" 以启用应用程序跟踪记录。如果 pageOutput="true",则
跟踪信息将显示在每一页的底部。否则,可以通过从 Web 应用程序
根浏览 "trace.axd" 页来查看
应用程序跟踪日志。
-->
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
<!-- 会话状态设置
默认情况下,ASP.NET 使用 cookie 标识哪些请求属于特定的会话。
如果 cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
若要禁用 cookie,请设置 sessionState cookieless="true"。
-->
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" />
<!-- 全球化
此节设置应用程序的全球化设置。
-->
<globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312"/>
</system.web>
</configuration>
private void Page_Load(object sender, System.EventArgs e)
{
//判断session的值是否为空,如果为空,则转到login.aspx
if(Session["adminName"]==null)
{
Response.Write("login.aspx");
Response.End();
}
}
<authentication mode="Forms">
<forms name="meigui" loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>在IIS添加mangage.aspx为起始页在“登陆”按钮事件中添加如下代码:if(IsValid)
{
string query;
query = "select * from t_usermanage where username='"+ UserName.Text +"' and (role='1000' or role='1100') ";//验证有无此用户
query += "and ";
query += "Password = '" + Password.Text + "'";
if (Login.userlogin(query))
{
FormsAuthentication.RedirectFromLoginPage(UserName.Text ,false);
Response.Redirect("mangage.aspx");
}
else
{
Label1.Text ="用户名或密码错误!";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
//判断session的值是否为空,如果为空,则转到login.aspx
if(Session["adminName"]==null)
{
Response.redirect("login.aspx");
Response.End();
}
}
Session["User"] = "admin";
判断session的方法:
if(Session["User"] == null || Session["User"] != "admin")
{
respose.redirect("index.aspx");
}
protected void Application_AcquireRequestState(Object sender, EventArgs E)
{ string[] URL = HttpContext.Current.Request.Url.Segments;
string strtemp=URL[URL.Length-1];
if((strtemp!="login.aspx" && strtemp!="index.aspx" )&&(Session["UserCode"] == null|| Session["UserName"] ==null))
{
Response.Redirect("login.aspx");//引导到首页或者出错页
}
}
你将不需要验证用户的页面放在strtemp!=后面,就可以了,其他的页面都会进行验证
Response.Write("login.aspx");
改成:
Response.Redirect("login.aspx");如果你的页面在框架中,可以这样:
Response.Write("<script>window.open('login.aspx','_top');</" + "script>");
所讲的,Form认证比较方便
此节设置应用程序的身份验证策略。可能的模式是“Windows”、“Forms”、
“Passport”和“None”
-->
<authentication mode="Forms">
<forms loginUrl="../longin.aspx" name=".ASPX" />//其作用是保护与“Web.config”同一目录的“*.aspx”文件在没有通过验证时直接跳转至上一层目录的“login.aspx”</authentication>
<authorization><deny users="?"/></authorization>
在“longin.aspx”的Page_Load事件里加上以下代码即可if(this.TextBox1.Text=="guest"&&this.TextBox2.Text=="123456")
{
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.username.Text,false); //将已验证身份的用户重定向回最初的页面
}
else
{
this.Label1.ForeColor=Color.Red;
this.Label1.Text="用户名不存在或密码错误。";
}如果你善于观察的话,就会发现地址栏“login.aspx?ReturnUrl=…………”
{
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false); //将已验证身份的用户重定向回最初的页面
}
else
{
this.Label1.ForeColor=Color.Red;
this.Label1.Text="用户名不存在或密码错误。";
}还有一种方法,不用Web.config限制.此方法的作用是只能从“login.aspx”进入“mangage.aspx”在mangage.aspx的Page_Load事件里写入以下代码if(!IsPostBack)
{
try
{
login mai = (login)Context.Handler; //从“main.aspx”中取得控制权
string name=mai.TextBox1.Text;
string pass=mai.TextBox2.Text;
if(name=="guest"&&pass=="123456")
{
}
else //没有通过验证
{
Response.Redirect("../longin.aspx");
}
}
catch //不是从“main.aspx”进入此页面
{
Response.Redirect("../longin.aspx");
}有时间看看我是怎么实现的:http://cityhunter.meibu.com(动态IP)上班时间开放