代码之家  ›  专栏  ›  技术社区  ›  Jim

ASP.NET-在默认情况下临时模拟应用程序池服务ID以模拟用户?

  •  2
  • Jim  · 技术社区  · 16 年前

    使用web应用程序的服务ID运行某些代码,然后恢复使用默认模拟的最佳方法是什么?

    4 回复  |  直到 16 年前
        1
  •  4
  •   Nat    16 年前
        2
  •  3
  •   Kirk Liemohn    16 年前

    纳特是对的。您应该使用SPSecurity.RunWithElevatedPrivileges。在封面下,它会还原Anthony提到的自己,但是使用helper方法要容易得多。可以使用内联委托,如下例所示。

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        // Your are now inside the delegate
        // Anything provided within this block is marshaled across the app domain
        using (SPSite site = new SPSite("http://myserver/sites/mysite"))
        {
            using (SPWeb web= site.OpenWeb())
            {
                // Do stuff here
            }
        }
    });
    
        3
  •  0
  •   AnthonyWJones    16 年前

    在ASP下,我有一个实用程序DLL,可以用来调用Win32的RevertoSelf()函数(位于advapi32.DLL中),以使ASP在应用程序池的标识下运行。

    当然,一旦出现,就无法返回线程使用的原始标识,但这并不是一个真正的问题。当前请求结束后,下一个请求将以用户身份(或匿名用户)再次运行。

        4
  •  0
  •   andrewbadera    16 年前

    容易的在HostingEnvironment.Impersonate()块中包装正在进行的调用。

    http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.impersonate.aspx