4

I have a SharePoint web application which contains two applications.

enter image description here

  • Sp.test.com => SharePoint web application, claims based authentication.
  • Employees => Employees application, authentication with AD account. This application access to SharePoint using the object model with the logged-in user.
  • Guests => Guests application, anonymous access. This application access to SharePoint using the object model with impersonation.

When access to SharePoint using the object model from Employees application, for example with user X, everything works correctly.

However, when access to SharePoint using the object model from Guest application, for example impersonating with user X, always get an access denied message.

Code used:

SPWeb webcon = null;

if (System.Web.HttpContext.Current != null)
    webcon = SPControl.GetContextWeb(System.Web.HttpContext.Current);

try
{
    int G_LCID = webcon.RegionalSettings.ServerLanguage.LCID;
}
catch (Exception ex)
{
    // Error
}

The error occurs accessing the property webcon.RegionalSettings.ServerLanguage.LCID

The property webcon.CurrentUser is null.

Error in SharePoint log:

Unknown SPRequest error occurred. More information: 0x80070005

SPRequest.OpenWeb: UserPrincipalName=, AppPrincipalName= ,bstrUrl=https://sp.test.com/Guests/default.aspx

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)), StackTrace: at Microsoft.SharePoint.SPWeb.InitWeb() at Microsoft.SharePoint.SPWeb.get_RegionalSettings() at _Default.Page_Load(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.default_aspx.ProcessRequest(HttpContext context) at System.Web.HttpAppl...

Thanks in advance.

1 Answer 1

3

You can try using SPSecurity.RunwithElevatedPrivilege for using impersonation in your case. it will elevate the access to the system account for running some chunk of the codes. below is the sample:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // do your stuffs
    using (SPSite site = new SPSite(web.Site.ID))
     {
        //Do things by assuming the permission of the "system account".
     }
});

More info : https://msdn.microsoft.com/library/Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.