简体   繁体   中英

How to allow SQL Server database connection from an ASP.NET web app and a Windows service?

I have an ASP.NET web application and the Windows service running on the same server machine. They both need to connect to the same database, using the following code:

using (SqlConnection cn = new SqlConnection(strDBConnect))
{
    cn.Open();

    //And so on...
}

But it gives me this exception when I try to connect from the service:

"Cannot open user default database. Login failed.\\r\\nLogin failed for user 'NT AUTHORITY\\SYSTEM'."

The connection string I'm using for both is, but it may be different on the actual deployment server:

data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\\Users\\User\\Databases\\App_Data\\Database1.mdf';User Instance=true;Connection Timeout=15

So I'm assuming that I need to add some sharing parameter into connection string. So my question is, how to ensure that more than one process can connect to the SQL Server database?

Create a specific user.

Use this user as the identity of the App Pool

Use this user for the identity for the service.

Add this user to sql server with the necessary rights.

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