安全地使用HttpContext.Current
来源:互联网 发布:centos移除用户 编辑:程序博客网 时间:2024/04/30 03:43
<>
安全地使用HttpContext.Current
有时我们会写些通用类库给ASP.NET或者WindowsService程序来使用,例如异常记录的工具方法。 对于ASP.NET程序来说,我们肯定希望在异常发生时,能记录URL,表单值,Cookie等等数据,便于事后分析。 然而对于WindowsService这类程序来说,您肯定没想过要记录Cookie吧? 那么如何实现一个通用的功能呢?
方法其实也简单,就是要判断HttpContext.Current是否返回null,例如下面的示例代码:
public static void LogException(Exception ex){ StringBuilder sb = new StringBuilder(); sb.Append("异常发生时间:").AppendLine(DateTime.Now.ToString()); sb.AppendLine(ex.ToString()); // 如果是ASP.NET程序,还需要记录URL,FORM, COOKIE之类的数据 HttpContext context = HttpContext.Current; if( context != null ) { // 能运行到这里,就肯定是在处理ASP.NET请求,我们可以放心地访问Request的所有数据 sb.AppendLine("Url:" + context.Request.RawUrl); // 还有记录什么数据,您自己来实现吧。 } System.IO.File.AppendAllText("日志文件路径", sb.ToString());}
就是一个判断,解决了所有问题,所以请忘记下面这类不安全的写法吧:
HttpContext.Current.Request.RawUrl;HttpContext.Current.Server.MapPath("xxxxxx");
下面的方法才是安全的:
HttpContext context = HttpContext.Current;if( context != null ) { // 在这里访问与请求有关的东西。}
0 0
- 安全地使用HttpContext.Current
- HttpContext.Current
- HttpContext.Current
- HttpContext.Current
- HttpContext.Current
- HttpContext.Current
- ASP.NET 使用HttpContext.Current.Cache
- 使用线程访问的类库使用httpcontext.current
- 在类中使用page对象用HttpContext.Current
- 在类中使用page对象用HttpContext.Current
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- mvc中尽量避免使用HttpContext.Current.Request
- abp项目中无法使用HttpContext.Current.Session[""]的问题
- ASP.NET多线程下使用HttpContext.Current为null解决方案
- HttpContext.Current.Items用途
- HttpContext.Current.Request.ServerVariables
- HttpContext.Current.Session是什么??
- HttpContext.Current并非无处不在
- Java EE应用的组件
- 你会为了钱出售自己的个人资料吗?
- android SDK问题:<dl-ssl.google.com> != <www.google.com>
- 突围“十面霾伏” 空气净化器成“黑马”
- hdu 1728 逃离迷宫 bfs记步数
- 安全地使用HttpContext.Current
- List源码解析
- 2.4. 请求对象
- 典型的信息系统生命周期模型
- SQL SERVER 2008安全配置
- 基于K-means算法的文本聚类
- php实例:打印金字塔
- tomcat服务器连接池知识
- MySQl Study学习之--MySQl二进制日志管理