构建安全高效的Microsoft ASP.NET 应用的最佳实践和技术

一、性能与缓存
*ASP.NET的性能状况 ASP.NET的引擎从原理上保证高性能 一次编译 Internet Explorer-->Parser-->Compiler-->Assembly Cashe-->Memory-->OutputCache-->回来 第二次运行 Internet Explorer-->-->Assembly Cashe-->Memory-->OutputCache-->回来

*ASP.NET的引擎从原理上保证高性能
CLR Just-in-time Compiler
对多CPU有很好的支持
运行时优化编译
*引擎的优化不能彻底解决性能问题
代码逻辑不优化
引擎无法控制的,潜在的性能优化点


*性能问题与优化原则
性能参数
吞吐量
响应时间
执行时间
可伸缩性

基本优化原则
减少不必要的资源消耗
CPU,内存

*性能提高的技巧
避免不必要的执行操作
Page_Load和IsPostBack
void Page_Load(Object sender,EventArgs e)
{
 if(!Page.IsPostBack){......}
}

*页面执行顺序
Page_Load-->Properties Change-->Action

*性能提高的技巧
关闭不必要的Session状态
<%@ Page EnableSessionState="false" %>
注意使用Server Control
不必要时可以不使用Server Control
不必要时可以关闭ViewState
<asp:datagrid EnableViewState="false" runat="server" />
<%@ Page EnableViewState="false" %>

*不要用Exception控制程序流程
try
{
 result=100/num;
}
catch(Exception e)
{
 result=0;
}
//--------------------------
if(num!=0)
 result=100/num;
else
 result=0;

*禁用VB和JScript动态数据类型
<%@ Page Language="VB" Strict="true" %>
使用存储过程数据访问
只读数据访问不要使用DataSet
使用SqlDataReader代替DataSet
SqlDataReader是read-only,forward-only
关闭ASP.NET的Debug模式
使用ASP.NET Output Cache缓冲数据

*ASP.NET输出缓冲
页面缓冲
<%@OutputCache %>
Duration
VaryByParam
片断缓冲
VarByControl
(用户空件)

*数据缓冲
过期依赖条件
Cache.Insert("MyData",Source,newCacheDependency(Server.MapPath("authors.xml")));
Cache.Insert("MyData",Source,null,DateTime.Now.AddHours(1),TimeSpan.Zero);
Cache.Insert("MyData",Source,null,DateTime.MaxValue,TimeSpan.FromMinutes(20));


二、ASP.NET应用安全事项

1、输入验证[前端验证/后台验证]

2、参数化查询

3、存储过程

4、html编码(防止javascript脚本执行) String.Format("Invalid Logon for {0},please try again!",Server.HtmlEncode(UserName));


认证与授权
配置管理
Review production configuration:
<customErrors> RemoteOnly or On
<compilation> disable debugging
Shared servers
Use configuration lockdown
<location allowOverride=“false”/>
Isolate by process (IIS 6) and/or with <trust> level
敏感数据
会话管理
使用加密
1、加密类CryPtography
2、随机类RNGCryptoServiceProvider
异常管理
最小权限

posted on 2010-01-13 17:41  bicabo  阅读(221)  评论(0编辑  收藏  举报

导航