【asp源码栏目提醒】:网学会员--在 asp源码编辑为广大网友搜集整理了:ASP NET安全性 - 计算机理论绩等信息,祝愿广大网友取得需要的信息,参考学习。
ASP.NET安全性 2007-08-26 20:59 by Yeemio 758 visits 网摘 收藏 编辑 学习MSDN的WEBCAST感觉单纯看一遍意义不大做个笔记以备不时只需查找方便。
代码部分本人在XPVS2005SQL2005测试通过请大家指教。
Asp.net安全性分为七个部分的议程。
如下。
?? 输入安全性 ?? 身份验证 ?? 授权 ??
ASP.NET模拟 ?? 存储机密 ?? 使用加密 ??
ASP.NET安全使用最佳实践 首先是输入安全性讲师讲解了SQL Injection的工作原理比如说应用中的模型是Query模型是 SELECT COUNT FROM Users WHERE UserName??Jeff??AND Password??imbatman??。
而恶意者在username对话框填写quotor 11--quot 这样就达到了一个恶意的Query的目的语句变成这样 SELECT COUNT FROM UsersWHERE UserName???? or 11-- 从而使password验证实效也就是可以把password验证给注释掉从而达到恶意的目的。
而且还可以通过SQL注入达到更可怕的目的举个例子吧对于这样的查询语句string strSql quotselect UserNameUserPass from tbUserInfo where UserNamequottbName.Textquot and UserPassquottbPass.Textquotquot可以这样的语句insert into tbUserInfo values张三111男quot--来达到写入数据表的目的。
对于这样呢我们可以通过验证输入来达到防范的目的1验证所有的输入一般情况下使用
asp.net的验证控件其他情况下使用正则表达式e.g.web server 参数。
2对于输出的数据要加密。
3使用参数化的存储过程和查询语句。
我们做一个例子来对比下有参数的和无参数的查询语句。
首先做页面放二个TEXTBOX一个输入用户名一个输入密码二个BUTTON一个是普通登录一个是安全登录。
代码如下 ltTABLE idquotTable1quot stylequotZ-INDEX: 102 LEFT: 182px POSITION: absolute TOP: 121pxquot cellSpacingquot1quot cellPaddingquot1quot widthquot300quot borderquot1quotgt ltTRgt ltTDgt ltasp:Label idquotLabel1quot runatquotserverquotgt用户名称lt/
asp:Labelgtlt/TDgt ltTDgt ltasp:TextBox idquottbNamequot runatquotserverquot Widthquot183pxquotgtlt/
asp:TextBoxgtlt/TDgt lt/TRgt ltTRgt ltTDgt ltasp:Label idquotLabel2quot runatquotserverquotgt密码lt/
asp:Labelgtlt/TDgt ltTDgt ltasp:TextBox idquottbPassquot runatquotserverquot Widthquot183pxquotgtlt/
asp:TextBoxgtlt/TDgt lt/TRgt lt/TABLEgt ltasp:Button idquotbtnLoginBetterquot stylequotZ-INDEX: 105 LEFT: 343px POSITION: absolute TOP: 226pxquot runatquotserverquot Widthquot78pxquot Textquot安全登录quot onclickquotbtnLoginBetter_Clickquotgtlt/
asp:Buttongt ltasp:Label idquotlbDiagquot stylequotZ-INDEX: 104 LEFT: 189px POSITION: absolute TOP: 343pxquot runatquotserverquot Widthquot599pxquotgtlt/
asp:Labelgt ltasp:Button idquotbtnLoginquot stylequotZ-INDEX: 100 LEFT: 213px POSITION: absolute TOP: 224pxquot runatquotserverquot Textquot登录quot Widthquot78pxquot onclickquotbtnLogin_Clickquotgtlt/
asp:Buttongt ltasp:Label idquotlbMsgquot stylequotZ-INDEX: 103 LEFT: 185px POSITION: absolute TOP: 282pxquot runatquotserverquot Widthquot449pxquotgtlt/
asp:Labelgt 在WEB.CONFIG里面添加连接字符串 ltconnectionStringsgt ltadd namequotDSNquot connectionStringquotserver.databasemyDatabaseuidsapwdquot/gt lt/connectionStringsgt 然后自己写验证语句代码如下 protected void btnLogin_Clickobject sender EventArgs e SqlConnection con new SqlConnection con.ConnectionString ConfigurationManager.ConnectionStringsquotDSNquot.ConnectionString con.Open //查询语句不带参数。
string strSql quotselect UserNameUserPass from tbUserInfo where UserNamequot tbName.Text quot and UserPassquot tbPass.Text quotquot SqlCommand sqlCom new SqlCommandstrSqlcon SqlDataReader dr sqlCom.ExecuteReader lbDiag.Text strSql //以下执行查询 bool bExist false whiledr.Read bExist true ifbExist lbMsg.Text quot您好quottbName.Text else lbMsg.Text tbName.Text quot不能进入quot con.Close protected void btnLoginBetter_Clickobject sender EventArgs e SqlConnection con new SqlConnection con.ConnectionString ConfigurationManager.ConnectionStringsquotDSNquot.ConnectionString con.Open //查询语句带参数。
string strSql quotselect UserNameUserPass from tbUserInfo where UserNameusername and UserPassuserpassquot SqlParameter sqlpUser new SqlParameterquotusernamequotSqlDbType.NVarChar30 sqlpUser.Value tbName.Text SqlParameter sqlpPass new SqlParameterquotuserpassquotSqlDbType.NVarChar30 sqlpPass.Value tbPass.Text SqlCommand com new SqlCommandstrSqlcon com.Parameters.AddsqlpUser com.Parameters.AddsqlpPass SqlDataReader dr com.ExecuteReader //以下执行查询 bool bExist false whiledr.Read bExist true lbDiag.Text strSql ifbExist lbMsg.Text quot您好quotServer.HtmlEncodetbName.Text else lbMsg.Text Server.HtmlEncodetbName.Text quot不能进入quot con.Close 有兴趣的朋友可以试验下两者的区别。
然后是身份验证 身份验证是指以下过程获取标识凭据如用户名和密码并对照某一颁发机构来验证这些凭据。
??
ASP.NET 提供了四个身份验证提供程序 – 表单身份验证 – Windows 身份验证 – Passport 身份验证 – 默认身份验证 请求的安全性事件流是这样的 1客户端请求aspx 页面。
2将客户端凭据传递给 IIS。
3IIS 对客户端进行身份验证然后将经过身份验证的标记随客户端请求一起传送到
ASP.NET 工作进程。
4根据 IIS 传送的经过身份验证的标记以及 Web 应用程序的配置设置
ASP.NET 决定是否在处理请求的线程上模拟用户。
下边对四种身份验证逐一说明。
1表单身份验证是指以下系统将未经身份验证的请求重定向到一个超文本标记语言HTML 表单使用能够在其中键入他们的凭据。
在用户提供凭据并提交该表单后应用程序对请求进行身份验证然后系统Cookie的形式发出身份验证票证。
此Cookie 包含凭据或用于重新获取标识的密钥。
浏览器的后续请求自动包含此Cookie。
2在Windows 身份验证中IIS 执行身份验证并将经过身份验证的标记传递给
ASP.NET 工作进程。
使用Windows 身份验证的优点是它需要的编码最少。
在将请求传递给
ASP.NET 之前您可能需要使用Windows 身份验证来模拟IIS 进行验证的Windows 用户帐户。
3Passport 身份验证是Microsoft 提供的集中式身份验证服务它为成员站点提供单一登录和核心配置文件服务。
通常当您需要跨越多个域的单一登录功能时将使用Passport 身份验证。
4当Web 应用程序不需要任何安全功能时将使用默认身份验证此安全提供程序需要匿名访问。
在所有身份验证提供程序中默认身份验证为应用程序提供了最高的性能。
当您使用自己的自定义安全模块时也可以使用此身份验证提供程序。
表单身份验证的开发步骤。
1. 将IIS 配置为使用匿名访问。
2. 将
ASP.NET 配置为使用表单身份验证。
3. 创建登录Web 表单并验证提供的凭据。
4. 从自定义数据存储中检索角色列表。
5. 创建表单身份验证票在票中存储角色。
6. 创建一个IPrincipal 对象。
7. 将IPrincipal 对象放到当前的HTTP 上下文中。
8. 基于用户名/角色成员身份对用户进行授权。
授权是指验证经身份验证的用户是否可以访问请求资源的过程。
??
ASP.NET 提供以下授权提供程序 – FileAuthorizationFileAuthorizationModule 类进行文件授权而且在使用Windows 身份验证时处于活动状态。
– UrlAuthorizationUrlAuthorizationModule 类进行统一资源定位器URL 授权它基于URI 命名空间来控制授权。
URI 命名空间可能与NTFS 权限使用的物理文件夹和文件路径存在很大的差异。
?? 若要建立访问特定目录的条件则必须将一个包含ltauthorizationgt 部分的配置文件放置在该目录中。
为该目录设置的条件也会应用到其子目录除非子目录中的配置文件重写这些条件。
此部分的常规语法如下所示。
ltelement users roles verbs/gt 元素是必需的。
必须包含users 或roles 属性。
可以同时包含二者但这不是必需的。
?? 以下示例向Kim 和管理角色的成员授予权限而拒绝John 和所有匿名用户 ltauthorizationgt ltallow usersquotKimquot/gt ltallow rolesquotAdminsquot/gt ltdeny usersquotJohnquot/gt ltdeny usersquotquot/gt lt/authorizationgt 若要允许John 并拒绝其他任何人可以构造下面的配置部分。
ltauthorizationgt ltallow usersquotJohnquot/gt ltdeny usersquotquot/gt lt/authorizationgt 下面的示例允许每个人使用GET但只有Kim 可以使用POST。
ltauthorizationgt ltallow verbquotGETquot usersquotquot/gt ltallow verbquotPOSTquot usersquotKimquot/gt ltdeny verbquotPOSTquot usersquotquot/gt lt/authorizationgt 关于表单验证也有一个例子首先我们再新建一个表不用上个例子那个表了为了因为这次用存储过程来做因为要使用加密字符串来操作。
use myDatabase go create table formsUi username varchar64 primary key passwordHash varchar50 not null passwordSalt varchar50 not null go 然后写存储过程 create procedure sp_getuserdetails acctname varchar64 passhash varchar50 out passsalt varchar50 out as select passhash passwordHash passsalt passwordSalt from formsUi where userName acctname go 数据库建立好了以后添加三个网页首先在web.config里添加这样的字段意思是起用表单验证。
ltauthentication modequotFormsquotgt ltforms namequot.SecurityDemoquot loginUrlquotlogin2.aspxquot gt lt/formsgt lt/authenticationgt 一个是注册页面注册时候就对密码进行加密 ltform idquotRegquot methodquotpostquot runatquotserverquotgt ltTABLE idquotTable1quot stylequotZ-INDEX: 101 LEFT: 167px WIDTH: 446px POSITION: absolute TOP: 159px HEIGHT: 72pxquot cellSpacingquot1quot cellPaddingquot1quot widthquot446quot borderquot1quotgt ltTRgt ltTDgt ltasp:Label idquotLabel1quot runatquotserverquotgt用户名称lt/
asp:Labelgtlt/TDgt ltTDgt ltasp:TextBox idquottbNamequot runatquotserverquot Widthquot183pxquotgtlt/
asp:TextBoxgtlt/TDgt ltTDgt ltasp:RequiredFieldValidator idquotRequiredFieldValidator1quot runatquotserverquot ControlToValidatequottbNamequot ErrorMessagequot用户名不能为空quotgtlt/
asp:RequiredFieldValidatorgtlt/TDgt lt/TRgt ltTRgt ltTDgt ltasp:Label idquotLabel2quot runatquotserverquotgt密码lt/
asp:Labelgtlt/TDgt ltTDgt ltasp:TextBox idquottbPassquot runatquotserverquot Widthquot183pxquotgtlt/
asp:TextBoxgtlt/TDgt ltTDgt ltasp:RequiredFieldValidator idquotRequiredFieldValidator2quot runatquotserverquot ControlToValidatequottbPassquot ErrorMessagequot密码不能为空quotgtlt/
asp:RequiredFieldValidatorgtlt/TDgt lt/TRgt lt/TABLEgt ltasp:Label idquotLabel3quot stylequotZ-INDEX: 102 LEFT: 228px POSITION: absolute TOP: 52pxquot runatquotserverquot Font-SizequotXX-Largequotgt用户注册页面lt/
asp:Labelgt ltasp:Button idquotbtnRegquot stylequotZ-INDEX: 103 LEFT: 322px POSITION: absolute TOP: 271pxquot runatquotserverquot Widthquot77pxquot Textquot注册quot onclickquotbtnReg_Clickquotgtlt/
asp:Buttongt lt/formgt 然后后台编码 using System using System.Collections using System.ComponentModel using System.Data using System.Drawing using System.Web using System.Web.SessionState using System.Web.UI using System.Web.UI.WebControls using System.Web.UI.HtmlControls using System.Configuration using System.Data.SqlClient using System.Security.Cryptography using System.Security using System.IO using System.Text namespace WebApplication1 public partial class reg : System.Web.UI.Page protected void Page_Loadobject sender EventArgs e protected void btnReg_Clickobject sender EventArgs e SqlConnection con new SqlConnection con.ConnectionStringConfigurationManager.ConnectionStringsquotDSNquot.ConnectionString con.Open //以下得到hash和salt加密串 const int salSize 16 // step 1: create some entropy for use as the salt RandomNumberGenerator rng RandomNumberGenerator.Create byte salt new bytesalSize rng.GetBytessalt // step 2: turn the password into bytes byte secret Encoding.Unicode.GetBytestbPass.Text // step 3: create the hash HashAlgorithm hashAlg SHA1.Create using CryptoStream cs new CryptoStreamStream.Null hashAlg CryptoStreamMode.Write cs.Writesecret 0 secret.Length cs.Writesalt 0 salt.Length cs.FlushFinalBlock string strHash Convert.ToBase64StringhashAlg.Hash string strSalt Convert.ToBase64Stringsalt // string strSql quotinsert into formsUi valuesusernamehashPasssaltPassquot SqlParameter sqlpUser new SqlParameterquotusernamequot SqlDbType.NVarChar 64 sqlpUser.Value tbName.Text SqlParameter sqlpPassHash new SqlParameterquothashPassquot SqlDbType.NVarChar 50 SqlParameter sqlpPassSalt new SqlParameterquotsaltPassquot SqlDbType.NVarChar 50 sqlpPassHash.Value strHash sqlpPassSalt.Value strSalt SqlCommand com new SqlCommandstrSql con com.Parameters.AddsqlpUser com.Parameters.AddsqlpPassHash com.Parameters.AddsqlpPassSalt com.ExecuteNonQuery con.Close Response.Writequotltscript languagejavascriptgtalert注册成功lt/scriptgtquot 一个网页做事例用也就是login2.aspx。
我们这样简单设计 ltform idquotloginquot methodquotpostquot runatquotserverquotgt ltTABLE idquotTable1quot stylequotZ-INDEX: 102 LEFT: 182px WIDTH: 446px POSITION: absolute TOP: 116px HEIGHT: 72pxquot cellSpacingquot1quot cellPaddingquot1quot widthquot446quot borderquot1quotgt ltTRgt ltTDgtltasp:label idquotLabel1quot runatquotserverquotgt用户名称lt/
asp:labelgtlt/TDgt ltTDgtltasp:textbox idquottbNamequot runatquotserverquot Widthquot183pxquotgtlt/
asp:textboxgtlt/TDgt ltTDgtltasp:requiredfieldvalidator idquotRequiredFieldValidator1quot runatquotserverquot ErrorMessagequot用户名不能为空quot ControlToValidatequottbNamequotgtlt/
asp:requiredfieldvalidatorgtlt/TDgt lt/TRgt ltTRgt ltTDgtltasp:label idquotLabel2quot runatquotserverquotgt密码lt/
asp:labelgtlt/TDgt ltTDgtltasp:textbox idquottbPassquot runatquotserverquot Widthquot183pxquotgtlt/
asp:textboxgtlt/TDgt ltTDgtltasp:requiredfieldvalidator idquotRequiredFieldValidator2quot runatquotserverquot ErrorMessagequot密码不能为空quot ControlToValidatequottbPassquotgtlt/
asp:requiredfieldvalidatorgtlt/TDgt lt/TRgt ltTRgt ltTDgtltFONT facequot宋体quotgt是否保存Cookielt/FONTgtlt/TDgt ltTDgtltasp:checkbox idquotPersistCookiequot runatquotserverquotgtlt/
asp:checkboxgtlt/TDgt ltTDgtlt/TDgt lt/TRgt lt/TABLEgt ltasp:button idquotbtnLoginBetterquot stylequotZ-INDEX: 105 LEFT: 312px POSITION: absolute TOP: 265pxquot runatquotserverquot Widthquot78pxquot Textquot登录quot onclickquotbtnLoginBetter_Clickquotgtlt/
asp:buttongtlt/formgt 然后编码这里MSDN给的
源码好像有问题就是随便输入用户名和密码都能通过问题出在数据流链接转换到加密输出的流那部分。
我做了下修改login2.aspx.cs: using System using System.Data using System.Configuration using System.Collections using System.Web using System.Web.Security using System.Web.UI using System.Web.UI.WebControls using System.Web.UI.WebControls.WebParts using System.Web.UI.HtmlControls using System.Data.SqlClient using System.Security.Cryptography using System.Text using System.IO using System.ComponentModel using System.Drawing using System.Web.SessionState namespace WebApplication1 public partial class login2 : System.Web.UI.Page protected void Page_Loadobject sender EventArgs e protected void btnLoginBetter_Clickobject sender EventArgs e bool bExist AuthenticateUsertbName.TexttbPass.Text ifbExist //1 //创建一个验证票据 FormsAuthenticationTicket ticket new FormsAuthenticationTicket1 tbName.TextDateTime.Now DateTime.Now.AddMinutes30PersistCookie.Checkedquotuserquot //2 //并且加密票据 string cookieStr FormsAuthentication.Encryptticket //3 创建cookie HttpCookie cookie new HttpCookieFormsAuthentication.FormsCookieNamecookieStr ifPersistCookie.Checked //如果用户选择了保存密码 cookie.Expiresticket.Expiration//设置cookie有效期 //cookie存放路径 cookie.Path FormsAuthentication.FormsCookiePath .