在vistual studio2005下使用cookies我们很容易就可以对网站进行 访问控制。
首选我们在web.config文件里,在system.web处添加authentication,验证模式选择Forms,..........具体如下:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Forms">
<forms name="loginform" loginUrl="login.aspx" protection="All"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
loginurl是登录页面的路径,这样在客户机没有cookies验证时会自动跳转到login.aspx页面,<deny users="?"/>
是拒绝匿名登录。
我们在登录页面通过System.Web.Security.FormsAuthentication.SetAuthCookie("ljjcookies", false);建立临时cookies,如果参数为true,那么是建立永久cookies,我们也可以使用System.Web.Security.FormsAuthentication.RedirectFromLoginPage("ljjcookies", true);这与上一个区别在于,后者会自动跳转到用户进入登录页之前的那页。使用System.Web.Security.FormsAuthentication.SignOut();就可以删除cookies。。。