文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET内置票据认证

ASP.NET内置票据认证

时间:2011-01-03  来源:卢青松

代码
 protected void Application_AuthenticateRequest(object SENDER, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id
= (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket tiecket
= id.Ticket;
string userData = tiecket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User
= new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}

2:  在web.config 文件中配置目录权限及登录页,

登录页,在system.web节点中

<authentication mode="Forms">
<forms name="mycook" loginUrl="login.aspx" protection="All" path="/"/>
</authentication>

 

配置目录权限,在system.web节点外面

代码
<location path="admin">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="user">
<system.web>
<authorization>
<allow roles="user"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="admin/admin_login.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="user/user_login.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

 

代码
 HttpCookie cook;
string strReturnURL;
FormsAuthenticationTicket ticket
= new FormsAuthenticationTicket(
1, name, DateTime.Now, DateTime.Now.AddMinutes(30), false, lv);
cook
= new HttpCookie("mycook");
cook.Value
= FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
strReturnURL
= Request.Params["ReturnUrl"];
if (strReturnURL != null)
{
Response.Redirect(strReturnURL);
}
else
{
Response.Redirect(
"Default.aspx");
}

 

 

 在登录页的登录事件中的登录成功后烤入一段代码

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载