1.web.config配置
2.login.aspx中验证方法中使用FormsAuthentication
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (Login1.UserName == "admin" && Login1.Password == "123456")
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
}
//我的webform中使用login控件
3.启用角色管理,在global.asax的
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (User != null &&User.Identity.IsAuthenticated)
{
if (System.Web.Security.Roles.Enabled == False)
{
FormsIdentity id = User.Identity as FormsIdentity;
string[] roles ={ "admin" };
GenericPrincipal principal = new GenericPrincipal(id, roles);
// 将新的主体对象附加到当前的 HttpContext 对象
HttpContext.Current.User = principal;
}
}
}
注意:如果想从其它数据源获得roles,可在登陆验证时用FormsAuthenticationTicket自定义User.Identity,
当然也可直接从数据源获得.^^网上有介绍