ASP.NET内置票据认证
根据你的设置,在进入到某一个目录下的页面时自动判断你是否有权限访问这个页面,没有权限则自动跳转到你预先设置的登录页
1、 在根目录建立一个Global.asax文件,复制如下一段代码
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>
3、 在登录页的登录事件中的登录成功后烤入一段代码
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");
}
根据你的设置,在进入到某一个目录下的页面时自动判断你是否有权限访问这个页面,没有权限则自动跳转到你预先设置的登录页
1、 在根目录建立一个Global.asax文件,复制如下一段代码
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>
3、 在登录页的登录事件中的登录成功后烤入一段代码
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");
}
发表评论
-
理解什么是IIS
2013-12-20 17:57 2561、IIS是网页服务器; 2、网页部署在IIS里我们才能通过浏 ... -
asp.net中的时间差问题
2013-11-27 17:28 391在asp中我们可以用datediff来处理,时间的差,相当的不 ... -
ASP.NET中进行队列消息处理(MSMQ)
2013-11-25 17:14 366网址:http://www.cnblogs.com/benia ... -
cookie购物车修改版
2013-11-17 23:16 331这可是修改版哦……NHibernate -
cookie购物车
2013-11-17 11:59 312用cookie实现购物车功能 -
asp.net关闭浏览器cookie购物车怎么清空
2013-11-14 10:05 547if (Request.Cookies["UserS ... -
牛腩新闻发布系统
2013-11-01 13:51 0这是牛腩的作品哦。 -
SQLHelper助手类
2013-10-28 12:45 287/* * 创建时间:2013-10-12 14:52 * ... -
上一条、下一条记录
2013-10-28 12:43 217前台页面: <span><font>上 ... -
每篇新闻的访问量
2013-10-28 12:36 285#region 修改访问量 /// <s ... -
分页功能
2013-10-28 12:33 286分页存储过程: set ANSI_NULLS ON set Q ... -
.net下拉列表式友情链接
2013-10-24 10:33 3341,前台页面代码: <asp ... -
下拉列表框加上说明
2013-10-23 22:52 435ddl.Items.Insert(0,new ListItem ... -
购物车里的加减号功能的实现代码
2013-09-27 12:39 970int num; protected void Pag ... -
写Page-Load事件时应注意
2013-09-14 20:55 323写Page-Load事件时最好加入: if(!IsPostB ... -
文本超出部分以…代替——摘自牛腩
2013-09-08 20:51 2661./// <summary> 2. ... -
为了后台安全,进入后台界面最好都先判断是否含有Session若没有跳回登录界面
2013-09-07 15:59 718例如: protected void Page_Load( ... -
Freetextbox文本框设置苹果皮肤和中文字体的属性
2013-09-07 10:11 315例: <FTB:FreeTextBox Text=&qu ... -
其实Freetextbox文本框提交出来的文本是含有html代码的文本,所以执行会出错
2013-09-07 10:09 319解决是在头部Page指令里加入ValidateRequest= ... -
根据session的值显示或者隐藏删除评论的按钮
2013-09-04 22:59 426<asp:LinkButton ID="lbt ...
相关推荐
详细介绍了asp.net内置对象和页面配置功能,能轻松的理解并运用
asp.net内置对象 asp.net内置对象 asp.net内置对象
Asp.Net内置对象,讲述了Asp.Net内置对象的基本属性的知识,对技术人员有帮助。
ASP.NET内置对象帮助我们更好的开发网页
细说ASP.NET Forms身份认证 阅读目录 • 开始 • ASP.NET身份认证基础 • ASP.NET身份认证过程 • 如何实现登录与注销 • 保护受限制的页面 • 登录页不能正常显示的问题 • 认识Forms身份认证 • 理解Forms身份认证...
ASP.Net内置对象的用法与区别,主要是ASP.NET里面的一些内置对象
ASP.NET内置对象,应用程序配置和缓存资料文档
讲解ASP的内置对象的文档资料 讲解ASP的内置对象的文档资料 讲解ASP的内置对象的文档资料
ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP.NET入门经典ASP...
asp.net Mvc项目中使用FastReport.NET的说明
1.理解ASP.NET内置对象的作用。 2.掌握Response内置对象的方法及属性。 3.掌握Request对象的用法。 4.掌握Session对象的用法。 5.熟悉Cookie对象的用法。 二.实验内容: (一).运用Response对象和Request对象...
关于asp.net的身份认证讲解。 web.config的身份认证
asp.net使用Form认证实现用户登录
ASP.NET的6个内置对象说明及简单的例子(PPT)
ASP.NET MVC5认证授权实例,ASP.NET MVC5认证授权实例
内容包括搭建ASP.NET开发环境、HTML开发与实践、CSS样式与JavaScript脚本应用、常用Web服务器控件、验证控件、高级Web服务器控件、ADO.NET技术、数据绑定技术、SQL查询相关技术、视图、存储过程及触发器的应用、ASP...