基于表单身份验证
l 表单身份验证的类(位于System.Web.Security):
1. FormsAuthentication-共享方法,用于验证。
2. FormsAuthenticationTicket-身份验证票据。
3. FormsIdentity—用户身份。
4. FormsAuthenticationModule-使用模块。
l 配置 Web 应用程序进行 Forms 身份验证
修改Web.Config
<authentication mode="Form">
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="AuthCookie" timeout="60" path="/">
</forms>
</authentication>
l 构造 GenericPrincipal 和 FormsIdentity 对象
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
基于表单身份验证
l 表单身份验证的类(位于System.Web.Security):
1. FormsAuthentication-共享方法,用于验证。
2. FormsAuthenticationTicket-身份验证票据。
3. FormsIdentity—用户身份。
4. FormsAuthenticationModule-使用模块。
l 配置 Web 应用程序进行 Forms 身份验证
修改Web.Config
<authentication mode="Form">
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="AuthCookie" timeout="60" path="/">
</forms>
</authentication>
l 构造 GenericPrincipal 和 FormsIdentity 对象
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
基于表单身份验证
l 表单身份验证的类(位于System.Web.Security):
1. FormsAuthentication-共享方法,用于验证。
2. FormsAuthenticationTicket-身份验证票据。
3. FormsIdentity—用户身份。
4. FormsAuthenticationModule-使用模块。
l 配置 Web 应用程序进行 Forms 身份验证
修改Web.Config
<authentication mode="Form">
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="AuthCookie" timeout="60" path="/">
</forms>
</authentication>
l 构造 GenericPrincipal 和 FormsIdentity 对象
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
using System.Web.Security;
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//请求传递的 cookie 集合中获得窗体身份验证 cookie。
string cookiename=FormsAuthentication.FormsCookieName;
HttpCookie cookie=Request.Cookies[cookiename];
if(cookie==null)
return;
//从窗体身份验证 cookie 中提取和解密身份验证票
FormsAuthenticationTicket ticket=null;
try
{
ticket=FormsAuthentication.Decrypt(cookie.Value);
}
catch(Exception err)
{
return ;
}
if(ticket==null)
return ;
//解析出用户在最初对用户进行身份验证时附加到票上的管道分隔的角色名称列表
string [] roles=ticket.UserData.Split(new char[] {'|'});
//创建一个 FormsIdentity 对象和一个 GenericPrincipal 对象。前一个对象从票名称获得用户名,后一个对象将此标识与用户角色列表包含在一起。
FormsIdentity ident=new FormsIdentity(ticket);
GenericPrincipal princ=new GenericPrincipal(ident,roles);
Context.User=princ;
}
- 验证及用户信息
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
FormsIdentity userIdentity;
FormsAuthenticationTicket objTicket;
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
{
//通过身份验证,显示一些说些,下面代码只是为了说明
userIdentity=User.Identity; //通过前面构造 GenericPrincipal 和 FormsIdentity 对象中已经赋值。
ObjTicket=userIdentity.Ticket;
//那么可以根据Ticket就可以获得信息
}
else
{
Response.Write("<script>alert('您没有登陆!');history.back()</script>");
//或者指向登录页面
//Response.Redirect("login.aspx");
}
}
- 身份验证的用户生成身份验证票
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}
{
//用户登陆验证
string ip=System.Web.HttpContext.Current.Request.UserHostAddress;
string name=username;
string pwd=password;
if(ValidatePwd(name,pwd)==true) //数据库验证,代码略
{
// 创建身份验证票证
FormsAuthenticationTicket ticker=new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddMinutes(30),false,"");
string encryptTicket=FormsAuthentication.Encrypt(ticker);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,encryptTicket);
Response.AppendCookie(cookie);
// 将用户重定向到最初请求页面。
Response.Redirect(FormsAuthentication.GetRedirectUrl(
name,false ));
// Response.Redirect(FormsAuthentication.RedirectFromLoginPage(name,false));
// this.Session["UserName"]=username;
}
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return "密码错误";
}
return "";
}