How to add windows authentication sql server connection string in Web Config?


I am currently using Entity Framework in my ASP.NET MVC web application, I would like to use windows authentication in SQL Server string, so how can do it?

Currently, I have web.Config configuration using username/password of SQL Server in the connection string as below

<add name="BlogSearchManagementContext" connectionString="metadata=res://*/BlogSearchManagement.csdl|res://*/BlogSearchManagement.ssdl|res://*/BlogSearchManagement.msl;provider=System.Data.SqlClient;provider connection string=&quot;server=tcp:etymon.database.windows.net,1433;initial catalog=testDBName;persist security info=True;user id=userid;password=password;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Thanks


Asked by:- neena
0
: 200 At:- 11/10/2021 11:45:08 AM
C# sql server connection string windows authentication sql server connection string






1 Answers
profileImage Answered by:- vikas_jk

You need to remove username/password and replace it with Integrated Security=SSPI; in your connection string, to use windows authentication, so considering your above example, it will look like below

    <add name="BlogSearchManagementContext" connectionString="metadata=res://*/BlogSearchManagement.csdl|res://*/BlogSearchManagement.ssdl|res://*/BlogSearchManagement.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-1PM1CJ9\SQLEXPRESS2;
initial catalog=dbName;
persist security info=True;
Integrated Security=SSPI;
MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

You can also check all type of SQL Server Connection String Example.

You can also make Integrated Security = true in the above code of Web.Config in connection string.

Setting Integrated Security = true means basically you want to reach database via Windows authentication, if you set this field false Windows authentication will not work.

To make it work with all other database, you may need to change it accordingly:

  • For OleDb it is Integrated Security=SSPI;
  • For Odbc it is Trusted_Connection=yes;
  • For OracleClient it is Integrated Security=yes;

That's it, hope it helps.

1
At:- 11/10/2021 12:31:16 PM
Thanks for the detailed answer, yes making integrated security: SSPI, it worked. 0
By : neena - at :- 11/10/2021 12:33:21 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use