Tuesday, 1 September 2009

Using Trusted Connections in Web Applications under SharePoint Server

Microsoft really try to hammer home the concept of using trusted connections in web.config files, such as



<connectionStrings>
<add name="MyDbConn1"
connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>
<add name="MyDbConn2"
connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"/>
</connectionStrings>


They will also mention that you do not need impersonation on for this to work, only that the identity account for the application pool has the required access to the SQL server (normally making this account a managed domain service account). This is despite many inccorrect postings on news groups saying you must have impersonation on (people never read the scenario)

However, there is one gotcha when playing your web application underneath a currently existing .NET application. If your top level website has



<identity impersonate="true">


then you will need to put



<identity impersonate="false">


into your own web.config to override (or override in another way, this worked best for me). This is the scenario you will face with WSS or MOSS, as all the sites attempt to impersonate the current user.

2 comments:

  1. If I turn impersonation off in the web config, my MOSS web app returns a "Cannot complete this action" error.

    Any ideas?

    ReplyDelete
  2. If you leave <identity impersonate="true" /> in the web.config (which SharePoint seems to like having left alone, thank you very much) but want to use the application pool's identity to access a resource (e.g. database connection), then you can use the following:

    using (HostingEnvironment.Impersonate())
    {

    // access external resource as app pool account

    }


    More info here: http://httpcode.com/blogs/PermaLink,guid,961b0893-cebf-47e2-a608-9562136bcad4.aspx

    ReplyDelete