0

I have a SharePoint site with anonymous access allowed.

I have a feature, that needs to access Shared Documents (both read and write) and hence when accessed it asks for login.

I want to implement global impersonation in a way that all users are auto-loggedin with a per-defined (preferably via Global.asax) account when they access the site so that they can perform what ever they want without having to login.

I don't care about the security issues as this is a test site I just want to make the user experience as simple as possible.

Once solution I already know is to share username and password on main page and let users use that to login. But I don't want users to perform an extra login step.

Urgent help is really appreciated.

1 Answer 1

0

please follow my explanation on this link:

SharePoint -access to path is denied

you dont need to change much and will grant the annoymous user access to any list/library without the need to login ;)

taken from the link above:

/// <summary>
/// get the system token from the list of users to elevate spsite
/// </summary>
/// <param name="spSite"></param>
/// <returns></returns>
protected static SPUserToken GetSystemToken(SPSite spSite) 
{     
    SPUserToken res = null;     
    bool oldCatchAccessDeniedException = spSite.CatchAccessDeniedException;
    try
    {
        spSite.CatchAccessDeniedException = false;
        res = spSite.SystemAccount.UserToken;
    }
    catch (UnauthorizedAccessException)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite elevatedSPSite = new SPSite(spSite.ID))
                res = elevatedSPSite.SystemAccount.UserToken;
            // (***)         
        });
    }
    finally    
    {         
        spSite.CatchAccessDeniedException = oldCatchAccessDeniedException;     
    }     
    return res; 
}

to use that function:

        SPUserToken sysToken = null;
        using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
        {
            //get the system token from the method GetSystemToken and passing the spSite that you want to run under
            sysToken = GetSystemToken(spSite);

            using (SPSite impersonatedSite = new SPSite(siteUrl, sysToken))
            {
                //we are impersonating the [@"SHAREPOINT\SYSTEM"] account
                using (SPWeb web = impersonatedSite.OpenWeb())
                {
                     //do your code here as the impersonated account like updating a list
                }
            }
        }

for detailed explanation please refer to the link!

Your Answer

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

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