ASP.NET 2.0 Internet安全之参考实现

[来源:J.D. Meier's Blog] 微软刚推出了一个ASP.NET 2.0 Internet 安全之参考实现( ASP.NET 2.0 Internet Security Reference Implementation)。这是个配有所有编码和指导性文档的样本应用,其宗旨是示范在实际应用中如何应用模式和实践之安全向导中的最佳实践。这个应用是从Pet Shop 4发展而来,使之适用于Internet。该应用使用了表单认证,用户和角色数据是储存在SQL数据库里的。 该应用能够在其官方网站上下载: ASP_NET 2_0 Internet Security Reference Implementation: Home http://www.gotdotnet.com/codegallery/codegallery.aspx?id=48f35de8-cd92-4ac6-9144-12d5a13f22ff [找不到连接] 下载的内容包括三部分 1。VS 2005方案和编码 2。Internet 安全参考实现的指导性文档 3。场景(Scenario)和方案文档 在安全参考实现的指导性文档里,涉及的设计决策包括下述分类 1。认证 2。受权 3。输入和数据验证 4。数据访问 5。异常管理 6。敏感数据(Sensitive Data) 7。审记和日志记录(Auditing and Logging) 在每一个分类里又具体列出了详细的设计决策,譬如,在认证方面,要作的决定包括 1。使用表单认证 2。使用SQL成员提供器 3。使用SSL来保护身份验证信息和认证cookies 4。不直接存储明文密码 5。强制使用安全性强的密码 6。保护对身份验证信息存储的访问 7。不除久认证cookies 8。在认证cookies上设置HttpOnly 9。使用独特的cookie名字和路径 对每个决定,又详细列出 1。是怎么实现的 2。这么作的缘由 3。好处 4。缺点 5。相关资源 涉及的方面不少,内容很是全,是一个学习设计/实现安全Web应用的好范例 Asp.Net安全验证小结 1,基于windows的安全验证 web.config文件: configuration system.web authentication mode="Windows" / identity impersonate="true" / authorization allow roles="BUILTIN\groupname" users="computername\UserName,computername\UserName" / deny users="*" / /authorization /system.web /configuration 在.aspx文件中无需任何代码就能够实现验证,但能够在.aspx文件获取登录用户的信息 需导入命名空间:System.Security.Principal if(User.Identity.IsAuthenticated)//判断用户是否验证,彷佛无关紧要 { WindowsIdentity objWinIdentity=WindowsIdentity.GetCurrent(); lblHelloMsg.Text="the name:"+objWinIdentity.Name+"brType:"+ objWinIdentity.AuthenticationType+"IsInRole:"+User.IsInRole("computername\\groupname"); } 2,基于web.config forms验证 web.config文件: configuration system.web authentication mode="Forms" forms name="MyApp" path="/" loginUrl="login.aspx" protection="All" timeout="30" credentials passwordFormat="Clear" user name="kwk" password="test" / user name="ljx" password="test" / /credentials /forms /authentication authorization allow users="kwk,ljx" / deny users="?" / /authorization /system.web /configuration login.aspx文件:须要提供两个文本框用于填写用户和密码(txtUsr,txtPwd),一个单选框判断是否永久保存 还须要一个按钮控件则响应该button的代码以下: void DoLogin(Object sender, EventArgs e) { if(FormsAuthentication.Authenticate(txtUsr.Value,txtPwd.Value)) { FormsAuthentication.RedirectFromLoginPage(txtUsr.Value,chkPersist.Checked); } else //为代码完整性而设置,能够不写 { Response.Write("authentication fails"); } } 而后在别的页面能够得到登录用户的值: if(User.Identity.IsAuthenticated)//能够不须要判断 { Response.Write("your name:"+User.Identity.Name); Response.Write("验证类型:"+User.Identity.AuthenticationType);//forms,windows等 } 3,基于自定义forms验证 web.config文件(基本上不须要什么设置): system.web authentication mode="Forms" forms name="MyApp" path="/" loginUrl="custom-login.aspx" protection="All" timeout="30" /forms /authentication authorization deny users="?" / /authorization /system.web custom-login.aspx文件,基本原理仍是跟2中说的同样,如: if (blnIsAuthenticated) //注意这个blnIsAuthenticated是一个本身定义的变量 //当咱们把用户输入的信息和数据库(或xml)的信息比对,存在则把该变量设为true,反之false //这是跟2不同的地方 { FormsAuthentication.RedirectFromLoginPage(txtUsr.Value, chkPersist.Checked); //txtUsr和chkPersist分别为textbox,checkbox控件 } else { //验证失败提示信息 } 剩下的如在其余页面得到用户信息,如2同样 4,退出登录 响应退出登录按钮的代码: FormsAuthentication.SignOut(); Response.Clear(); Response.Redirect(Request.UrlReferrer.ToString());//重定向到前一个页面
相关文章
相关标签/搜索