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

I have to call some code in a SharePoint site that runs under the same service ID that the web application is running under. By default, SharePoint impersonates the user viewing the web page, and the users don't have the necessary permissions.

What is the best way to run some code using the web application's service ID, then revert back to using the default impersonation?

#1

Nat is right. You should use SPSecurity.RunWithElevatedPrivileges. Under the covers it does a ReverToSelf that Anthony mentions, but it is much easier to use the helper method. You can use an inline delegate as in the following example.

The main thing to realize is that this delegate runs under a separate application domain which basically means that you want to use a SPSite or SPWeb you must re-instantiate them within the delegate as shown below.

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
        }
    }
});

#2

Under ASP I had a utility DLL that I could use to call Win32's RevertToSelf() function (found in the advapi32.dll) to get the ASP to run under the identity of the application pool.

Of course once there, there's no going back to the original identity the thread was using but that's not really a problem. Once the current request had ended, the next request would run again under the users identity (or the anonymous users).

You could probably do the same with PInvoke in ASP.NET but I wouldn't know what effect that might have on the framework. I'm certain it would last only for the current request. I don't think there is any standard .NET API to do this.

#3

easy! Wrap the calls you're making in a HostingEnvironment.Impersonate() block.

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

推荐文章

为什么app.config中的connectionstringcollection的条目比connection Strings部分的条目多?

为什么app.config中的connectionstringcollection的条目比connection Strings部分的条目多?

推荐文章

有没有可能在ASP.NET中使用证书加强表单身份验证?

有没有可能在ASP.NET中使用证书加强表单身份验证?

推荐文章

浮点运算不产生精确结果

浮点运算不产生精确结果

推荐文章

在不修改boto文件的情况下禁用boto日志记录

在不修改boto文件的情况下禁用boto日志记录

推荐文章

可以为多个WCF服务协定添加一个服务引用

可以为多个WCF服务协定添加一个服务引用

推荐文章

ASP.NET MVC-存储/服务/控制器

ASP.NET MVC-存储/服务/控制器

推荐文章

数据源不支持服务器端数据分页

数据源不支持服务器端数据分页

推荐文章

为什么ListView项不会增长以包装其内容?

为什么ListView项不会增长以包装其内容?

推荐文章

增加下拉列表的宽度

增加下拉列表的宽度

推荐文章

与Jython一起美化团队

与Jython一起美化团队

推荐文章

当内容溢出页面时自动滚动的ScrollViewer高度

当内容溢出页面时自动滚动的ScrollViewer高度

推荐文章

如何在js.file中包含来自外部文件的js函数?

如何在js.file中包含来自外部文件的js函数?

推荐文章

打开的文件句柄太多

打开的文件句柄太多

推荐文章

SimpleDateFormat和基于区域设置的格式字符串

SimpleDateFormat和基于区域设置的格式字符串

推荐文章

在VB.net 2008中从串行端口读取信息

在VB.net 2008中从串行端口读取信息

推荐文章

冷熔渣

冷熔渣