Question

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" />


Sponsored



Replies

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...

Solution:

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:-

http://www.c-sharpcorner.com/uploadfile/specialhost/how-to-fix-login-failed-for-user-nt-authority-iusrs-in-share/

MARK AS ANSWER IF IT HELPS!

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


Hi,

 

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:

SPSecurity.RunWithElevatedPrivileges(delegate()

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

                        SqlConnection sqlConn = new SqlConnection(_Conn);

                        using (sqlConn)
                        {
                            sqlConn.Open();

                            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

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


Hi,

Is any update for your issue?

Best Regards,

Lee