Search


Meta

Creative


Support


Leesoft

Feed™

Recent Comments

Photos

May
28

NET安全编程无责任指南:DoNotIndirectlyExposeMethodsWithLinkDemands

Posted by » developerly
47 hits

今天利用FxCop对工作中代码进行分析,代码片段如下:

private static FileStream CreateFileStream(string fName, string stream, FileAccess access)
{
SafeFileHandle handleValue = NativeMethods.CreateFile(string.Format(”{0}:{1}”, fName, stream),
Convert(desiredAccess),
access== FileAccess.Read ? NativeMethods.FILE_SHARE_READ : 0,
IntPtr.Zero,
NativeMethods.OPEN_ALWAYS,
0,
IntPtr.Zero);

if (handleValue.IsInvalid)
{

return null;
}

return new FileStream(handleValue, desiredAccess);
}

分析结果是[Warning]Do not indirectly expose methods with link demands。具体来看因为使用了Microsoft.Win32.SafeHandles.SafeFileHandle的IsInvalid方法。这个方法使用了LinkDemand签名因为它是一个非托管的方法。对于调用使用了LinkDemand声明的方法。对于此类方法只有完全受新人的调用方才能使用他们,除非程序集通过应用AllowPartiallyTruestedCaller属性显式的决定参与使用。这样避免了非受信任的代码通过反射等方式来调用此类方法。

因此要解决上述问题,需要对函数CreateFileStream进行声明

我使用 [System.Security.Permissions.PermissionSetAttribute
(System.Security.Permissions.SecurityAction.Demand, Name=”FullTrust”)]
来表明函数是安全的。具体的描述可以参考如下链接:

http://www.host01.com/article/Net/c/056609494910420_3.htm

May
16

用wix打包的时候需要注意的问题

Posted by » developerly
90 hits

wix是微软某个高人写的基于命令行的MSI打包工具。通过编写基于XML的wxs文件,可以对安装过程进行精确控制。同时可以通过编写CustomAction并加入到InstallExecuteSequence节点下完成一些打包过程中所需要的外部操作,比如读写注册表,写eventlog,创建新文件夹等。

对于如下的CustomAction

<CustomAction Id=’InstallXXXService’
Directory=’INSTALLDIR’
ExeCommand=”[\”][SystemFolder]cmd.exe[\”] /d /c [\”][INSTALLDIR]XXXX.exe[\”] -install > demuxerinstall.log 2>&1″
Execute=’deferred’
TerminalServerAware=’yes’
/>

并加入到如下的 InstallExecuteSequence
<InstallExecuteSequence>
<Custom Action=’InstallXXXService’ After=’InstallFinalize’><![CDATA[Not Installed]]></Custom>
</InstallExecuteSequence>

安装时会提示:
DEBUG: Error 2613: InstallXXXService action sequenced incorrectly.
The installer has encountered an unexpected error installing this package. This
may indicate a problem with this package. The error code is 2613. The arguments
are: , ,

解决方案: 如果需要在InstallFinalize之后执行的CustomAction操作,需要把 Execute属性改成immediate

May
15

全民大讨论:由于SxsUninstallCA导致卸载包含VC++代码的.NET程序异常缓慢

Posted by » developerly
149 hits

这是一个在开发中遇到的问题。然后在网上搜到很多的讨论,翻译汇编如下。

我的VS 2005安装程序的安装和卸载在windows vista下面都没有问题,但是不能在windows XP下面正常卸载。基本上来说,如果我在windows xp下面卸载,在卸载的最后(程序文件都已经删除的时候),卸载对话框却不能关闭。而且也没有任何出错信息。没有办法,我只好在任务管理器里面杀掉这个进程。大家有什么好的主意吗?谢谢
– Mario M.


跳转后阅读全民大讨论:由于SxsUninstallCA导致卸载包含VC++代码的.NET程序异常缓慢的完整内容

May
7

今天参加.NET技术培训了

Posted by » developerly
46 hits

今天参加公司组织的.NET技术培训了,关于.NET下面性能调优方面的知识的。讲师是来自bonyond的一个微软的MVP。一直以来我在技术上是一个杂家了,所以虽然这个blog涉及到的.NET技术不是很多,不过我还是下过功夫钻研过的。有机会我会结合具体的工作,把一些心得放上来。

今天讲师演示了VS2008中Ajax extension的应用。没想到微软的Ajax实现框架还是很笨重的。页面中每次Ajax组件刷新,都会重新调用page_onload方法,等于是重新调用页面对应的cs文件。貌似不是很符合常人的思维啊。

Mar
28

Windows Performance计数器,权限的奥义

Posted by » developerly
97 hits

最近研究windows performance计数器。运行在windows2003系统上,代码方面很简单:

PerformanceCounterCategory.Create(myCategoryName, “”, PerformanceCounterCategoryType.MultiInstance, new_counter);

如果使用Admin用户执行这段代码一点问题都没有。现在问题主要在于,这个程序不能以Admin用户权限来运行。普通用户在使用PerformanceCounterCategory创建性能计数器的时候会提示注册表权限不够。
“System.Security.SecurityExceptionRequested registry access is not allowed”
想当然的就为注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services添加了相应的用户,并赋予了full control的权限。
再运行程序,提示:
“System.ComponentModel.Win32ExceptionAccess is denied”
这次提示不再是注册表的问题了,而是系统访问权限不够。经过,一番调查,也试过把相关用户加到performance monitor usergroup和performance log usergorup里面,都没有解决这个问题,最终在如下网址里面关于windows2003的评论里找到了答案:http://www.objectsharp.com/cs/blogs/bruce/archive/2003/12/05/222.aspx
跳转后阅读Windows Performance计数器,权限的奥义的完整内容