// 一、在配置文件web.config中添加以下节点
<authentication mode="Forms">
<forms name="form1" loginUrl="/login.aspx" protection="All" timeout="30"
path="/"/>
</authentication>
// 二、在登录成功后设置登录的信息
string[] roles = { "会员" };
IIdentity identity = new GenericIdentity(LoginName);
IPrincipal principal = new GenericPrincipal(identity, roles);
System.Web.HttpContext Context = System.Web.HttpContext.Current;
Context.User = principal;
FormsAuthentication.SetAuthCookie(LoginName, false);
(注:引入using System.Web.Security 与 using System.Security.Principal命名空间)
// 三、通过以下方式可以判断是否已登录
if (!this.Context.User.Identity.IsAuthenticated || this.Context.User.Identity == null || this.Context.User == null)
{
Response.Redirect("Login.aspx");
return;
}
// 四、退出登录状态
FormsAuthentication.SignOut();
是不是很简单,以上测试在VS2008中成功通过