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="server=tcp:etymon.database.windows.net,1433;initial catalog=testDBName;persist security info=True;user id=userid;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Thanks
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="data source=DESKTOP-1PM1CJ9\SQLEXPRESS2;
initial catalog=dbName;
persist security info=True;
Integrated Security=SSPI;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
For .NET Core
{
"ConnectionStrings": {
"DefaultConnection": "Server=SQLServer\\Instance;Database=MYDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
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:
That's it, hope it helps.
You don't need Persist Security also, so here shorter version of Windows Authentication SQL Server Connection string in .NET/Web.Config
<connectionStrings>
<add name="DBConnection"
connectionString="data source=SQLSERVER\MYINSTANCE;
Initial Catalog=MyDB;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
in ASP.NET Core Appsettings.JSON
{
"ConnectionStrings": {
"DefaultConnection": "Server=SQLServer\\Instance;Database=MYDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
and for JDBC client Windows Authentication connection string can be
String url ="jdbc:sqlserver://PC01\instance01;databaseName=MYDB;integratedSecurity=true";
in Python
conn_str = (
r'Driver=SQL Server;'
r'Server=.\SQLEXPRESS;'
r'Database=myDB;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
Using PHP
/* Specify the server and connection string attributes. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
Hope it helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly