web.config 里的经验
1.由于是web系统,所以要求用户必须登录才可以查看,所以匿名用户是不可以访问的。于是在web.config里做如下设置
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<system.web>
<!--采用Forms验证-->
<authentication mode="Forms">
<forms loginUrl="LoginIn.aspx" name=".ASPXAUTH" protection="All" timeout="5"></forms>
</authentication>
<!--拒绝未登录用户-->
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
这里稍微做下解释吧:
<authentication mode="Forms“ > 这里是采用forms来验证用户是否登录,
<forms loginUrl="LoginIn.aspx" name=".ASPXAUTH" protection="All" timeout="5">
如果是没有登录的,则转向 loginUrl="LoginIn.aspx" 页面进行登录,
name=".ASPXAUTH" 记录该用户的cookie名称
protection="All" 加密方式
timeout="5" 保存多少失效,单位分钟
<deny users="?"></deny> 拒绝匿名用户访问。未登录的
web.config里弄好了以后,我们就可以保护我们的web应用必须登录才能使用。
2.当页面出错,让页面自动跳转至指定页面
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/AllSystem/S_Error.htm">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
</system.web>
//注意:由于我是分开写的,所以加上<system.web>,而在实际的web.config里的 它们应该是一起的,所以在复制的时候注意下。
通过以上的设置,即可。
3.说说我昨天的问题
我按照上面的做法,把验证方式改成Form,拒绝匿名登录,然后把S_Error.htm放入到另外一个文件夹叫AllSystem
(以前是放在jscss里的,这里有个web.config允许匿名访问) ,接下来,
我要改正页面的判断,把业务逻辑转到biz层(因为之前写到view层),
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
public static void LoginInLoadCheck(string t,string name,Label lblCompany, Page p)
{
if (t == null || name == null)
{
HttpContext.Current.Response.Redirect("~/AllSystem/S_Error.htm?key=" + LogSystem.NotPremission);//就是这里5次F5后才能执行跳转,而且跳的还不对
}
else
{
string nname = Md5.MD5Decrypt(name);
if (nname.Equals(""))
{
HttpContext.Current.Response.Redirect("~/AllSystem/S_Error.htm?key=" + LogSystem.ParameterError);
}
lblCompany.Text = nname;
p.Title = lblCompany.Text + "欢迎你使用···";
}
}
后,浏览器报出无法显示该页,后来一步步调试,转到此处时,按了5次F5 才跳转,我就非常郁闷,因为就是一个简单
跳转,也会出现问题。试了很多次,都不行。重新把代码放到view层,也还是不行,更郁闷了,,,,
后来想到是权限问题,才想起来,由于是第一次访问,所以得让S_Error.htm允许任何人访问,不然的话,一辈子你也进不了
于是在AllSystem的文件 夹里添加一个web.config,(也有其他的办法,就是用<location>来进行配置,由于时间紧,没来得及做case)
注:每个文件夹里的web.config只限于该文件夹,不限于其他文件夹,
即可。web.config代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来
配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“ASP.NET 配置”
选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于 \Windows\Microsoft.NET\Framework\v2.0.xxxxx\Config 中
-->
<configuration>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
菜鸟文章,欢迎拍砖。:)