Connection string for custom web part

Category: apps for sharepoint 2013


Tweety Tan on Thu, 11 Aug 2016 03:14:53

i am using window authentication for my sql connection. if seems that the connection to sql keep using NT AUTHORITY\IUSR.

my application pool has already being set to use another identity and it seems this is not being pick

connection string

<add name="DBConnection" connectionString="Data Source=localhost;Integrated Security=True;Initial Catalog=myDB;Connect Timeout=30;Persist Security Info=true;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />


Shekhar.Diary on Thu, 11 Aug 2016 06:13:25

Hi Tweety,

Your website is running within an app pool, the app pool is using certain user credentials, in your case NT AUTHORITY\\IUSR.

This means if you configured your database connection string to use integrated authentication the DB connection will use the app pool's user to connect to the database...


Either give permissions to the app pool's user to be able to access your database
or use a different user for your app pool which has permissions already
or specify user/pw in your DB connection string instead of using integrated auth.

Try  this:-


Tweety Tan on Thu, 11 Aug 2016 10:04:37

Hi Shekhar,

i have check and my application pool  and the indentity is the credentials that it is suppose to be using. somehow the custom web part keep using NT AUTHORITY\\IUSR. i found out that when i set the ASP.Net Impersonation in my IIS, the connection worked.

Lee__li on Fri, 12 Aug 2016 05:53:21



You can use  SPSecurity.RunWithElevatedPrivileges to run the code which runs under the Application Pool identity.

If your App Pool identity has access to DB, it should work.

Attached my sample code:


                        string _Conn = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

                        SqlConnection sqlConn = new SqlConnection(_Conn);

                        using (sqlConn)

                            SqlCommand command = new SqlCommand("select count(1) from student", sqlConn);

                            DataSet ds = new DataSet();

                            Label1.Text = "DB Result:" + command.ExecuteReader().FieldCount.ToString();                            



Best Regards,


Lee__li on Wed, 17 Aug 2016 08:17:23


Is any update for your issue?

Best Regards,