| Michael's profileMike HackerBlogLists | Help |
|
21 August SharePoint User Profile Access IssuesToday I will feature a problem recently encountered with updating and reading user profile information when connected to SharePoint as an anonymous user. The Task The Problem During testing of this solution I still continued to receive access denied errors when the exists method of the UserProfileManager object was executed. This made no sense since the application pool account had full permissions to make modification to the user profile store. (Note: when using RunWithElevatedPriviliges under a forms authenticated site the actual account used is the application pool account). The Cause Since this class is protected under Microsoft copyright I will not be reproducing the actual code but I will explain at a high level what the GetCurrentUserName method does and why it caused the access denied errors. The GetCurrentUserName class inspects the HttpContext.Current property and if it is not null it uses it to determine the current user and validates that the user can be authenticated and then returns the username. If the HttpContext.Current property is null then the current WindowsIdentity is used to determine the current user and then returns that username. So back to our problem… the access denied error. When using RunWithElevatedPriviliges impersonation takes place and the WindowsIdentity object now returns the SharePoint system account (which in our case is the application pool account). RunWithElevatedPriviliges does not modify the HttpContext, so in our case since our user is not authenticated the HttpContext.Current.User property is set to an empty string. This means that when the GetCurrentUserName method from the UserProfileGlobal class executes it sees the the HttpContext.Current property is not null and then attempts to validate the username which is an empty string. This causes the method to return an access denied error. The Workaround To prevent the ASP.NET page from no longer working it is important to store the value of the current context in a variable prior to setting the current context to null so that way it can be restored when done reading or writing from the user profile store. Below is an example.
HttpContext myContext = HttpContext.Current;
HttpContext.Current = null;
…do profile read / writes here … HttpContext.Current = myContext;
Remember that the above code is run within a RunWithElevatedPriviliges code block. It is also important to make sure you do not try to use any objects or methods that need access to the HttpContext object between the time you set the current context to null and when you restore it to the original value. Results Comments (8)
TrackbacksWeblogs that reference this entry
|
|
|