简体   繁体   中英

Changing connection string from SQL Server to windows authentication

I have to change from SQL Server authentication to Windows authentication:

My SQL Server authentication string is:

 <add name="GCDataContext" 
      connectionString="Data Source=111.78.152.64;Initial Catalog=GC;User Id=sa;Password=xxxx;Trusted_Connection=False;Persist Security Info=True;MultipleActiveResultSets=True" 
      providerName="System.Data.SqlClient" />

Changed to (not working)

 <add name="GCDataContext" 
      connectionString="Data Source=111.78.152.64;Initial Catalog=GC;Persist Security Info=True;Integrated Security=True;" 
      providerName="System.Data.SqlClient" />

Can anyone please help me on this?

Integrated Security=True is for Windows authentication, but the connection is not established.

<add name="GCDataContext" 
      connectionString="Data Source=111.78.152.64;Initial Catalog=GC;Integrated Security=True" 
      providerName="System.Data.SqlClient" />  

This should work.

You just need to remove Persist Security Info=True
Also you can copy my above code

ASP.NET applications do not impersonate by default. As a result, when they use Windows authentication to connect to SQL Server, they use the Web application's process identity. With this approach, your front-end Web application authenticates and authorizes its users and then uses a trusted identity to access the database. The database trusts the application's identity and trusts the application to properly authenticate and authorize callers.

To connect to SQL Server using Windows authentication, perform the following steps:

Step 1. Configure a connection string. The connection string used with Windows authentication must include either the Trusted_Connection=Yes attribute, or the equivalent attribute Integrated Security=SSPI,

Step 2. Configure SQL Server security: You need to create a SQL Server login for your application's service account, and grant restricted permissions to access your database. You application's service account is usually either the Network Service account, which is the default account used to run ASP.NET application pools on Windows Server 2003 and IIS 6.0 or a custom service account.

For this do the following-

                 a).Create a SQL Server login for your application's account.
                 b).Map the login to a database user in the required database.
                 c). Provide appropriate permissions

<add name="GCDataContext" connectionString="Data Source=111.78.152.64;Initial Catalog=GC;Integrated Security=True;" providerName="System.Data.SqlClient" />

Remove : Persist Security Info=True; If it's not able to connect verify DB permission and privilege.

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