Microsoft KB Archive/892866

= Impersonation does not work when you use the SharePoint Portal Server object model or the Windows SharePoint Services object model to impersonate an authenticated user =

Article ID: 892866

Article Last Modified on 5/23/2007

-

APPLIES TO


 * Microsoft Office SharePoint Portal Server 2003
 * Microsoft Windows SharePoint Services

-



SYMPTOMS
You write code that uses the Microsoft Office SharePoint Portal Server object model or the Microsoft Windows SharePoint Services object model to try to impersonate an authenticated user in one or both of the following contexts:
 * In the context of a Web Part
 * In the context of a program that runs under an anonymous user or of a program that runs under a non-authenticated user

However, impersonation does not work with some SharePoint Portal Server object model calls or with some Windows SharePoint Services object model calls. You may receive an error message that is similar to the following:

Access is Denied

For example, you may experience this symptom in the following scenario:  Impersonation code is called from a Web part or from console code that runs under an anonymous user. Impersonation code is called to make sure that an administrator account is used in this context. The call to the SPWeb.Roles collection in this scenario fails.  The following code is run: SPSite siteCollection = new SPSite(&quot;http://localhost&quot;); SPWeb web2 = siteCollection.OpenWeb; SPRole adminRole = web2.Roles[&quot;Administrator&quot;];  The access to the web2.Roles collection fails.



CAUSE
Impersonation is not currently supported in the SharePoint Portal Server object model or in the Windows SharePoint Services object model. This functionality is being investigated for future versions of the object models.



MORE INFORMATION
For more information about code that you can use to impersonate a different Microsoft Windows user, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsidentity.impersonate(vs.71).aspx

For more information about the SharePoint Portal Server object model, visit the following Microsoft Web site:

http://www.microsoft.com/technet/prodtechnol/sppt/reskit/c3461881x.mspx

For more information about the Windows SharePoint Services object model, see the Microsoft SharePoint Products and Technologies 2003 Software Development Kit (SDK). To obtain the SharePoint Products and Technologies 2003 SDK, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?FamilyID=aa3e7fe5-daee-4d10-980f-789b827967b0&displaylang=en

Keywords: kbprogramming kberrmsg kbprb KB892866

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.