C# 我可以仅在几种情况下关闭模拟吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/125096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:42:04  来源:igfitidea点击:

Can I turn off impersonation just in a couple instances

提问by naspinski

I have an app that has impersonation used throughout. But when a user is logged in as an admin, a few operation require them to write to the server itself. Now if these users do not have rights on the actual server (some don't) it will not let them write.

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

What I want to do is turn off impersonation for just a couple commands.

我想要做的是仅针对几个命令关闭模拟。

Is there a way to do something like this?

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

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

Thank you.

谢谢你。

采纳答案by Maxime Rouiller

Make sure the Application Pool do have the proper rights that you need.

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

Then, when you want to revert to the application pool identity... run the following:

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

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 { }
}

回答by jedatu

I am not sure if this is the preferred approach but when I wanted to do this I new'd up an instance of a WindowsIdentityand called the Impersonatemethod. This allows subsequent code to impersonate a different Windows user. It returns a WindowsImpersonationContextthat has an Undomethod which reverts the impersonation context back again.

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

回答by Guvante

You could turn off authentication for the page and then manually impersonate the authenticated user during the remainder of your code.

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

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

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

This has a reference to that last part, but basically you impersonate User.Identity

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

This will mean you will have to impersonate at the beginning of any call to the page, turn it off when you need it off, then turn it back on when you are done, but it should be a workable solution.

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

回答by naspinski

I just ended up giving the folders write permissions to "Authenticated Users"

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