2010年03月 小记(兼容模式, 时间格式, iexplore.exe, CSRF)

1、 WCF中的使用与ASPNET兼容模式

在开发WCF的服务中,如果使用到ASP.NET的特有上下文特性,如HttpContext.Current.Items等,请必须使用与ASPNET兼容模式。
# 需要在定义ServiceContract中指定AspNetCompatibilityRequirementsMode.Allowed,如:
[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class PermissionCheckerProvider : IPermissionChecker

#在服务端的web.config中配置:
<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

</system.serviceModel>

2、如何正确解析"yyyyMMddHHmmss"时间格式

对于"yyyyMMddHHmmss"这种时间格式(如:20100326163224),通过DateTime.Parse方法是不能正确解析的,因为考虑了本地时间格式,需要通过新的方法解析:

DateTime.ParseExact(string s, string format, IFormatProvider provider)

如:

string nowStr = DateTime.Now.ToString("yyyyMMddHHmmss");

DateTime dt = DateTime.ParseExact(nowStr, "yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);

这里的DateTimeFormatInfo.InvariantInfo为忽略本地系统时间格式 

或者使用

DateTime.TryParseExact(nowStr, "yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo,DateTimeStyles.None, out  dt); 


3、让IE8支持多用户登录。

-private 可以创建一个私有的会话信息(包括cookies, temporary Internet files, history, and other data. Toolbars and extensions are disabled by default.) 。命令如下:

"C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging -private 

可以通过创建一个快捷方式来简化些操作 

4、Prevent Cross-Site Request Forgery (CSRF)

ASP.NET MVC内置了CSRF功能,使用很简单 

#在页面的form中放置,如: <%= Html.AntiForgeryToken("salt string")%>

这样页面就生成一个类拟

<input name="__RequestVerificationToken" type="hidden" value="TwDsfrm...A=" /> 的隐藏字段,同时会向cookie中写入名为__RequestVerificationToken的cookie,这样在用户请求post到服务端的时候就自动匹配这两个自段,判断是否为合法请求

#服务端,在action上添加[HttpPost, ValidateAntiForgeryToken(Salt = "salt string")] 就可以自动判别了。如:

[HttpPost, ValidateAntiForgeryToken(Salt = "salt string")]

public ActionResult UploadImage(HttpPostedFileBase fileUpload)

#注意,如果设置了Salt,则界面和后参代码都要使用相同的salt字段串

原文地址:https://www.cnblogs.com/chenjunbiao/p/1760296.html