(转)基于窗体的身份验证

基于窗体的身份验证

//登陆页面

protected void Button1_Click(object sender, EventArgs e)
{
 string username = "tsuser";
 string userpwd = "resust";
 string roles = "Adminstrator";
   
 //生成验证票据对象.
 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
  1,
  username,
  DateTime.Now, DateTime.Now.AddMinutes(1),
  false,
  roles);

 //加密验证票
 string encrytedTicket = FormsAuthentication.Encrypt(authTicket);
 //生成Cookie对象.
 //FormsAuthentication.FormsCookieName取得WebConfig中<Authentication>
 //配置节中Name的值作为Cookie的名字.
 HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
  encrytedTicket);
 Response.Cookies.Add(authCookie);

 //跳转到用户的初试请求页.
 string ddd = FormsAuthentication.GetRedirectUrl(username, false);

 Response.Redirect( "WebForm1.aspx" );
 //Server.Transfer("WebForm1.aspx");
}

//Global.asax中《加上using System.Security.Principal;命名空间》
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
 //获取用户的角色。
 string cookieName = FormsAuthentication.FormsCookieName;//从验证票据获取Cookie的名字。

 //取得Cookie.
 HttpCookie authCookie = Context.Request.Cookies[cookieName];

 if (null == authCookie)
 {
  return;
 }
 FormsAuthenticationTicket authTicket = null;

 //获取验证票据。
 authTicket = FormsAuthentication.Decrypt(authCookie.Value);

 if (null == authTicket)
 {
  return;
 }

 //验证票据的UserData中存放的是用户角色信息。
 //UserData本来存放用户自定义信息。此处用来存放用户角色。
 string[] roles = authTicket.UserData.Split(new char[] { ',' });

 FormsIdentity id = new FormsIdentity(authTicket);

 GenericPrincipal principal = new GenericPrincipal(id, roles);

 //把生成的验证票信息和角色信息赋给当前用户.
 Context.User = principal;
}

//不通过验证直接进入主页,会返回到default.aspx页面*/

//也可以在新页面中判断

if ( !HttpContext.Current.User.IsInRole("Adminstrator") )
{
 Response.Redirect("Default.aspx");
}


/*//web.config中添加
<authentication mode="Forms">
 <forms name="MYWEBAPP.ASPXAUTH"
 loginUrl="default.aspx"
 protection="All"
 path="/"/>
 </authentication>
 <!-- 设置对本地目录的访问。如果验证票据未通过,则无法访问
 -->
 <authorization>
  <!-- Order and case are important below -->
  <allow roles="Adminstrator"/>
  <deny users="*"/>
 </authorization>

基于窗体的身份验证是一项 ASP.NET 身份验证服务,它使应用程序能够提供自己的登录用户界面并进行自己的凭据验证。ASP.NET 对用户进行身份验证,将未经身份验证的用户重定向到登录页,并执行所有必要的 Cookie 管理。这种身份验证是许多网站使用的流行方法。

应用程序必须配置为使用基于窗体的身份验证,方法是将 <authentication> 设置为 Forms,并拒绝匿名用户访问。下面的示例演示如何在所需应用程序的 Web.config 文件中完成此操作:

<configuration><system.web><authentication mode="Forms"/><authorization><deny users="?" /></authorization></system.web></configuration>
管理员使用基于窗体的身份验证来配置要使用的 Cookie 的名称、保护类型、用于登录页的 URL、Cookie 有效的时间长度,以及用于已发出的 Cookie 的路径。下表显示 <Forms> 元素的有效属性,该元素是下面的示例中显示的 <authentication> 元素的子元素:

<authentication mode="Forms"><forms name=".ASPXCOOKIEDEMO" loginUrl="login.aspx" defaultUrl="default.aspx"protection="All" timeout="30" path="/" requireSSL="false"slidingExpiration="true" enableCrossAppRedirects="false"cookieless="UseDeviceProfile" domain=""><!-- protection="[All|None|Encryption|Validation]" --><!-- cookieless="[UseUri | UseCookies | AutoDetect | UseDeviceProfile]" --></forms></authentication>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值