简体   繁体   中英

SQL Server error in IIS asp.net web page

Good day everyone! I've been having a problem, and I figure it out that I'm not the only one with this 'kind' of SQL Exception. But, I done everything that I found and with the same result. So, please, let me introduce my problem.

I've been working in asp.net web application. Developed in Visual Studio 2015 (community version). In my project I have some SQL Connection to my local database (in the same computer that my project). Everything is going well when I click "Debug" (with my favorite browser). I can make querys (select, inset, update, etc). Now I want to make it public in my intranet (my cowerkers want to wwork with this app as well). So, I moved my project folder to

C:\\inetpub\\wwwroot

And yes, the IIS web site is configured (correctly, I think, well I have another intranet apps working fine!)

The Default page in my web page is a Login.aspx and when I try to access with the correct keys (username and password), SQL shows me an error.

Invalid login attempt. Database issue.System.Data.SqlClient.SqlException (0x80131904): Error de inicio de sesión del usuario 'IIS APPPOOL\\SADI'. en System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) en System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) en System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionIn ternal oldConnection) en System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio nSource 1 retry, DbConnectionOptions userOptions) en System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) en System.Data.SqlClient.SqlConnection.Open() en SADMIN.Login.btnLog_Click(Object sender, EventArgs e) en C:\\Users\\MyUser\\Documents\\Visual Studio 2015\\Projects\\MyProject\\MyProject\\Login.aspx.cs:line 43 ClientConnectionId:6cc4ef30-4720-4c45-ba87-d85f39804685 Error Number:18456,State:1,Class:14 Enviar

And if your're wondering... This is my line 43:

conn.Open();

And this my BackCode in the Login.aspx

  try
                 {

                     SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security
 Info=False;User ID=sa;Initial Catalog=MyDatabase;Data
 Source=PCUsername\SQLInstance");
                     SqlCommand cmd = new SqlCommand();
                     SqlDataReader reader;

                     cmd.CommandText = "spcLoginValidate";
                    cmd.CommandType = CommandType.StoredProcedure;
                     cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = txtusername.Text;
                     cmd.Parameters.Add("@passw", SqlDbType.VarChar).Value = txtpassword.Text;

                     cmd.Connection = conn;
                     conn.Open();

                     reader = cmd.ExecuteReader();
                     while (reader.Read())
                     {
                         idRole = Convert.ToInt32(reader[0].ToString());
                         Session["User"] = reader[1].ToString();
                         Session["ID"] = idRole;

                     }



                     reader.Close();
                     conn.Close();

                     lblError.Visible = true;
                 }
                 catch (Exception ex)
                 {
                     lblError.Text = "Invalid login attempt. Database issue." + ex;
                     lblError.Visible = true;

                 }

In the Catch section I have the labelError whith the SQL Exception, and in that label is where my web page shows me the error that I gave you before.

My SQL Server services are running, so I don´t know what to do! Any help from you guys it would be awesome! I've been trying to resolve this issue for 3 days, and still have it.

You need to grant your IIS AppPool\\SADI account access to the DB or disable integrated security and add a password for the sa user account.

Creating SQL Server Logins and DB Users: https://technet.microsoft.com/en-us/library/aa337545(v=sql.110).aspx

The error is self explaining. Your application pool account does not have sufficient rights on the SQL database.

You may:

  1. change the identity of your application to a domain account, and grant privileges to this account on the DB
  2. change SQL authentication to user/password and create a sql login (not recommended though)

Also please check if user impersonation is disabled in the web.config file (this may lead to such issue if not)

Well, after 4 days searching for information about this issue. And still don't know the reason.

I found what I need, after all, this resolved my problem. I just ran this sentence in SQL Server's query (inside my database)

 exec sp_grantlogin 'IIS APPPOOL\DefaultAppPool'  
 use yourDB exec
 sp_grantdbaccess 'IIS APPPOOL\DefaultAppPool'

Special thanks to Moho and Steve B, your answers were fundamental to found the resolution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM