19

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

我想要做的只是关闭几个命令的模拟。

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

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

谢谢你。

4

4 回答 4

24

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

然后,当您想要恢复到应用程序池身份时...运行以下命令:

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 { }
}
于 2008-10-20T03:25:02.403 回答
2

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

于 2008-09-24T03:14:19.837 回答
0

您可以关闭页面的身份验证,然后在代码的其余部分手动模拟经过身份验证的用户。

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

这有对最后一部分的引用,但基本上你冒充 User.Identity

这意味着您必须在对页面的任何调用开始时模拟,在需要时将其关闭,然后在完成后重新打开,但这应该是一个可行的解决方案。

于 2008-09-24T03:43:14.853 回答
0

我刚刚将文件夹写入权限授予“经过身份验证的用户”

于 2008-09-24T05:33:00.690 回答