我可以closures模​​拟,只是在几个实例

我有一个应用程序在整个模仿使用。 但是,当一个用户以pipe理员身份login时,一些操作需要他们自己写入服务器。 现在,如果这些用户在实际的服务器上没有权限(有些不),它不会让他们写。

我想要做的就是closures模拟只是几个命令。

有没有办法做这样的事情?

using(HostingEnvironment.Impersonate.Off()) //I know this isn't a command, but you get the idea? 

谢谢。

确保应用程序池确实拥有您所需的适当权限。

然后,当您想要恢复到应用程序池标识时,请运行以下命令:

 private WindowsImpersonationContext context = null; public void RevertToAppPool() { try { if (!WindowsIdentity.GetCurrent().IsSystem) { context = WindowsIdentity.Impersonate(System.IntPtr.Zero); } } catch { } } public void UndoImpersonation() { try { if (context != null) { context.Undo(); } } catch { } } 

我不确定这是否是首选的方法,但是当我想要这样做时,我新build了一个WindowsIdentity的实例,并调用了Impersonate方法。 这允许后续代码模拟不同的Windows用户。 它返回一个WindowsImpersonationContext ,它具有一个Undo方法,可以将模拟上下文再次恢复。

您可以closures页面的身份validation,然后在代码的其余部分手动模拟已通过身份validation的用户。

http://support.microsoft.com/kb/306158

这有一个参考,最后一部分,但基本上你模仿User.Identity

这意味着你必须在页面的任何调用开始时模仿,当你需要的时候把它关掉,然后在你完成时把它重新打开,但这应该是一个可行的解决scheme。

我刚刚结束了给文件夹写入权限“已validation的用户”