1、创建一个网站,结构如下:
网站根目录
Admin目录 ---->
管理员目录
Manager.aspx ----> 管理员可以访问的页面
Users目录 ---->
注册用户目录
Welcome.aspx ----> 注册用户可以访问的页面
Error目录 ---->
错误提示目录
AccessError.htm ----> 访问错误的提示页面
default.aspx ----> 网站默认页面
login.aspx ----> 网站登录页面
web.config ----> 网站配置文件
2、配置web.config如下:
<configuration>
<system.web>
<!--设置Forms身份验证-->
<authentication mode="Forms">
<forms loginUrl="Login.aspx"name="MyWebApp.APSXAUTH" path="/"protection="All" timeout="30"/>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
<!--设置Admin目录的访问权限-->
<location path="Admin">
<system.web>
<authorization>
<allowroles="Admin"/>
<denyusers="?"/>
</authorization>
</system.web>
</location>
<!--设置Users目录的访问权限-->
<location path="Users">
<system.web>
<authorization>
<allowroles="User"/>
<denyusers="?"/>
</authorization>
</system.web>
</location>
3、在login.aspx页面的登录部分代码如下:
protected void btnLogin_Click(object sender, EventArgs e)
{
//Forms身份验证初始化
FormsAuthentication.Initialize();
//验证用户输入并得到登录用户,txtName是用户名称,txtPassword是登录密码
UserModel um =ValidUser(txtName.Text.Trim(),txtPassword.Text.Trim());
if (um != null)
{
//创建身份验证票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
um.Name,
DateTime.Now,
DateTime.Now.AddMinutes(30),
true,
um.Roles,//用户所属的角色字符串
FormsAuthentication.FormsCookiePath);
//加密身份验证票据
string hash =FormsAuthentication.Encrypt(ticket);
//创建要发送到客户端的cookie
HttpCookie cookie =
New HttpCookie(FormsAuthentication.FormsCookieName, hash);
if(ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
//把准备好的cookie加入到响应流中
Response.Cookies.Add(cookie);
//转发到请求的页面
Response.Redirect(FormsAuthentication.GetRedirectUrl(um.Name,false));
}
else
{
ClientScriptManagercsm = this.Page.ClientScript;
csm.RegisterStartupScript(this.GetType(),"error_tip", "alert('用户名或密码错误!身份验证失败!');", true);
}
}
//验证用户
private UserModel ValidUser(string name, string password)
{
return newUserService().Validate(name, password);
}
4、给网站添加处理程序Global.asax,其中通用身份验证代码如下:
//改造原来的User,给其添加一个用户所属的角色数据
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 ticket =id.Ticket;
string userData = ticket.UserData;
string[] roles = userData.Split(',');
//重建HttpContext.Current.User,加入用户拥有的角色数组
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}
5、在Admin目录中Manager.aspx页面加载代码如下:
复制代码代码如下:
protected void Page_Load(object sender, EventArgs e)
{
//判断通过身份验证的用户是否有权限访问本页面
FormsIdentity id =(FormsIdentity)HttpContext.Current.User.Identity;
//判断通过身份验证的用户是否是Admin角色
if(!id.Ticket.UserData.Contains("Admin"))
{
//跳转到访问权限不够的错误提示页面
Response.Redirect("~/Error/AccessError.htm",true);
}
}
//安全退出按钮的代码
protected void btnExit_Click(object sender, EventArgs e)
{
//注销票据
FormsAuthentication.SignOut();
ClientScriptManager csm =this.Page.ClientScript;
csm.RegisterStartupScript(this.GetType(), "exit_tip","alert('您已经安全退出了!');", true);
}
6、在Users目录中Welcome.aspx页面加载代码如下:
protected void Page_Load(object sender, EventArgs e)
{
//判断通过身份验证的用户是否有权限访问本页面
FormsIdentity id =(FormsIdentity)HttpContext.Current.User.Identity;
//判断通过身份验证的用户是否是User角色
if(!id.Ticket.UserData.Contains("User"))
{
//跳转到访问权限不够的错误提示页面
Response.Redirect("~/Error/AccessError.htm", true);
}
}
//安全退出按钮的代码
protected void btnExit_Click(object sender, EventArgs e)
{
//注销票据
FormsAuthentication.SignOut();
ClientScriptManager csm =this.Page.ClientScript;
csm.RegisterStartupScript(this.GetType(), "exit_tip","alert('您已经安全退出了!');", true);
}
测试结果:
数据:
假设有3个用户,如下:
------------------------------------------
用户名密码角色字符串
------------------------------------------
sa sa Admin,User
admin admin Admin
user user User
------------------------------------------
测试:
如果使用admin登录,只能访问Admin目录的Manager.aspx页面;
如果使用user登录,只能访问Users目录的Welcome.aspx页面;
使用sa登录,既能访问Admin目录的Manager.aspx页面,又能访问Users目录的Welcome.aspx页面。
注意:测试时注意及时点击安全退出按钮,否则影响测试结果。
分享到:
相关推荐
NULL 博文链接:https://ihhb.iteye.com/blog/813533
一个系统中经常有多种身份的用户,往往要根据其身份来控制目录的访问权限。asp.net提供了forms验证,能够轻易的在配置文件中设置用户对目录的访问权限.
Forms身份验证用来判断是否合法用户,当用户合法后,再通过用户的角色决定能访问的页面。
一般网站都有权限管理,登录验证,这个demo可以拿来做参考,简单实用
它基于员工管理系统DEMO的解决方案。...用Forms进行身份验证 列出所有用户。 创建用户。 编辑用户。 删除用户。 在创建用户时为用户提供角色 稍后改变角色 根据用户角色显示不同的导航菜单(在布局视图中使用部分视图)
ASP.NET Forms验证 实现子域名(SubDomain)共享登陆 http://www.cnblogs.com/Medi-Bruce/archive/2010/01/17/1650027.html 一、什么是单点登录 单点登录就是在多个web应用程序中,实现统一登录方式,一但登录了某...
您可以使用由 IIS 提供的 Windows 身份验证对用户进行身份验证,也可以通过您自己的用户数据库使用 ASP.NET Forms 身份验证和 ASP.NET 成员资格来管理身份验证。此外,可以使用 Windows 组或您自己的自定义角色...
Coalesys PanelBar + R.a.d Treeview +Xml 构建的Asp.net 菜单和权限管理模块 突破屏蔽限制,自己的网站使劲弹新IE窗口 对页面SCROLLING的CSS不能生效原因 .Net 中IE使用WinForm控件的使用心得。 动态加载用户控件的...
ch14\Example_14_2~ Example_14_5 ---- 第14章中的数据库安全和ASP.NET中的安全,如防止SQL注入攻击、使用信任连接、使用存储过程控制数据库的访问和使用ASP.NET中的Forms验证方式; ch14\DBSecurity ---- 第14章中...
.net提供了3种用户认证的方式,分别是Windows,Forms,Passport。这几种形式的定义可以在网站根目录下Web.config中的authentication节点中看见。Windows是默认的验证形式,它是根据机器的访问权限来判断的。
但是,我们在实际的应用中所使用的大都是基于角色(Roles)的认证方式,NeedDinner中却未给出,本文给出具体实现(基于ASP.NET Forms验证)过程: step 1在完成UserName和Password认证后,向客户端写
ASP.NET中如何防范SQL注入式攻击 一、什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令。在某些表单中,用户输入的...
3. **用户身份验证**:集成了ASP.NET的身份验证和授权机制,确保只有合法用户才能访问聊天室和进行文件传输操作。支持用户注册、登录和权限管理等功能。 4. **界面友好、易于定制**:采用了现代化的Web界面设计,...
**计算机专业毕设:ASP.NET某店积分更新记录管理...通过学习和实践本项目,您可以掌握ASP.NET Web Forms框架的应用技巧,积累项目经验,提升自己的软件开发能力。同时,本项目也可作为您进行二次开发或定制的基础平台。
ch14\Example_14_2~ Example_14_5 ---- 第14章中的数据库安全和ASP.NET中的安全,如防止SQL注入攻击、使用信任连接、使用存储过程控制数据库的访问和使用ASP.NET中的Forms验证方式; ch14\DBSecurity ---- 第14章中...
系统采用ASP.NET Web Forms或ASP.NET Core作为开发框架,利用C#编程语言进行后端开发。数据库方面,支持SQL Server、MySQL等多种关系型数据库。前端则采用了HTML、CSS和JavaScript等现代Web技术,实现了响应式布局和...
源代码采用C#语言编写,基于ASP.NET Web Forms框架。论文部分详细介绍了项目的背景、需求分析、系统设计、实现与测试过程,以及项目成果和创新点等内容。这些资料对于学习和研究ASP.NET Web开发、网络通信和软件工程...
ASP.NET 中提供了多种认证方式,比如大名鼎鼎的Windows 身份验证 Windows 身份验证模式根据 IIS 所提供的凭据将当前 User 属性值设置为 WindowsIdentity,但它不修改提供给操作系统的 Windows 标识。提供给操作系统...