When writing web parts and such for SharePoint 2010 it is common to have the need to access external SQL databases. Since SharePoint 2010 runs with ASP.NET Impersonation enabled (required for many of the web services and social features to work), it will not use the Application Pool user account when logging into SQL. This results in a login failed when trying to connect to SQL:

[shell]Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON'[/shell]

To allow your custom solution to access the external SQL database, you have a couple of options. The less ideal option is to include a SQL account in your connection string. The better solution is to run that section of code with elevated privileges. By running the section of code accessing SQL with elevated privileges, it forces the connection to authenticate with the Application Pool user account rather than anonymous.

[csharp]SPSecurity.RunWithElevatedPrivileges(() =>
// Your code goes here

Hope this saves you some headaches!

Pin It on Pinterest

Share This
%d bloggers like this: