SQL Server connection string to use windows account [duplicate] - sql-server

I am creating a website, but in the database I use windows authentication.
I know that you use this for SQL authentication
<connectionStrings>
<add name="NorthwindContex"
connectionString="data source=localhost;
initial catalog=northwind;persist security info=True;
user id=sa;password=P#ssw0rd"
providerName="System.Data.SqlClient" />
</connectionStrings>
How do I modify this to work with windows authentication?

Replace the username and password with Integrated Security=SSPI;
So the connection string should be
<connectionStrings>
<add name="NorthwindContex"
connectionString="data source=localhost;
initial catalog=northwind;persist security info=True;
Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>

For connecting to a sql server database via Windows authentication basically needs which server you want to connect , what is your database name , Integrated Security info and provider name.
Basically this works:
<connectionStrings>
<add name="MyConnectionString"
connectionString="data source=ServerName;
Initial Catalog=DatabaseName;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Setting Integrated Security field true means basically you want to reach database via Windows authentication, if you set this field false Windows authentication will not work.
It is also working different according which provider you are using.
SqlClient both Integrated Security=true; or IntegratedSecurity=SSPI; is working.
OleDb it is Integrated Security=SSPI;
Odbc it is Trusted_Connection=yes;
OracleClient it is Integrated Security=yes;
Integrated Security=true throws an exception when used with the OleDb provider.

For the correct solution after many hours:
Open the configuration file
Change the connection string with the following
<add name="umbracoDbDSN" connectionString="data source=YOUR_SERVER_NAME;database=nrc;Integrated Security=SSPI;persist security info=True;" providerName="System.Data.SqlClient" />
Change the YOUR_SERVER_NAME with your current server name and save
Open the IIS Manager
Find the name of the application pool that the website or web application is using
Right-click and choose Advanced settings
From Advanced settings under Process Model change the Identity to Custom account and add your Server Admin details, please see the attached images:
Hope this will help.

This is shorter and works
<connectionStrings>
<add name="DBConnection"
connectionString="data source=SERVER\INSTANCE;
Initial Catalog=MyDB;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Persist Security Info not needed

If anyone comes looking for asp.net core, we will have to add connection string in appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=SQLServer\\Instance;Database=MYDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Source: add windows authentication sql server connection string

Related

Multiple connection string with different users in my web.config?

I have my web.config like this:
<connectionStrings>
<add name="myConnectionString"
connectionString="Data Source=PC1;
Initial Catalog=BD1;
Integrated Security=SSPI"
providerName="System.Data.SqlClient"/>
<add name="myConnectionString2"
connectionString="Data Source=PC2;
Initial Catalog=BD2;
Integrated Security=False;
User Id=usuario;
Password=pass"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I can connect to the first one, a database with my windows user auth, but i can not to the second one, a database with other windows user auth.
Error: Login failed for user 'usuario'.
I can run Sql Server as other user, and i can connect to the database.
Picture how i run SQL server

Login failed for user 'test'

I'm trying to use bellow method to initialize my database which is hosted on one of Microsoft Azure's servers.
WebSecurity.InitializeDatabaseConnection("DefaultConnection","UserPacient", "PacientID", "PacientUsername", true);
The connection string looks like this
<add name="DefaultConnection" connectionString="Data Source=servername;Initial Catalog=DBName;Persist Security Info=True;User ID=username;Password=***********" providerName="System.Data.SqlClient" />
When I ran the application it throws
Login failed for user 'HelpMed'.
Any idea on why this error??
I need to specify that from SSMS I can connect and access the database from Azure...

ASP.NET MVC deployment problems with connection string

I have just deployed one mvc application, but it seems that it can't establish proper connection to the database. So during development I was connecting to the database with the following connection string(using windows authentication):
<connectionStrings>
<add name="PicknickDBEntities"
connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;
provider connection string="
data source=**-**-**\SQLEXPRESS;
initial catalog=PicknickDB;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
Now my app will go on a machine (with installed SQL Server 2008) with the name VB-app which uses SQL authentication with usrnm and passin order to connect to the database. Any ideas what should I change in my connection string to accommodate the new properties?
Replace
integrated security = true
with
User Id=usrnm;
Password=passin;
Remove integrated security=True;
and add
User Id=myUsername;
Password=myPassword;
where you change myUsername and myPassoword to the right username & password
It will look like this:
<connectionStrings>
<add name="PicknickDBEntities"
connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;
provider connection string="
data source=**-**-**\SQLEXPRESS;
initial catalog=PicknickDB;
User Id=myUsername;
Password=myPassword;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />

Unable to Connect with SQL Server Authentication

I'm creating a simple ASP.NET MVC 4 application using Entity Framework and code-first. With SQL Server Express 2012, everything works fine when using Windows Authentication:
<add name="dbConnectionString" connectionString="Data Source=(localdb)\v11.0; AttachDbFilename=|DataDirectory|myDb.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
However, when using the following for SQL Server Authentication:
<add name="dbConnectionString" connectionString="Data Source=(localdb)\v11.0; AttachDbFilename=|DataDirectory|myDb.mdf;Integrated Security=False; User Id=mysa; Password=mysapassword" providerName="System.Data.SqlClient" />
I receive an error:
"Login failed for user 'mysa'" ProviderIncompatibleException.
Am I missing something?
Is it possible that your *.mdf file is not attached to sql server instance?
Try attaching the myDb.mdf file to your database with SQL Management Studio and the change the connection string to
<add name="dbConnectionString" connectionString="Data Source=(localdb)\v11.0; Category=myDb;Integrated Security=False; User=mysa; Password=mysapassword" providerName="System.Data.SqlClient" />
and don't forget to give the appropriate rights to your user

Generate SQL Server Express database from Entity Framework 4 model

I am able to auto-generate a SQL Server CE 4.0 *.sdf file using code-first generation as explained by Scott Guthrie here. The connection string for the same is as follows:
<add name="NerdDinners" providerName="System.Data.SqlServerCe.4.0"
connectionString="data source=|DataDirectory|NerdDinner.sdf"/>
However if I try to generate an mdf instead using the following connection string, it fails to do so with the following error - "The provider did not return a ProviderManifestToken string.".
<add name="NerdDinners" providerName="System.Data.SqlClient" connectionString="data
source=|DataDirectory|NerdDinner.mdf"/>
Even directly hooking into a SQLEXPRESS instance using the following connection string fails
<add name="NerdDinners" providerName="System.Data.SqlClient" connectionString="Data
Source=.\SQLEXPRESS;Initial Catalog=NerdDinner;Integrated Security=True"/>
Does EF 4 only support SQL CE 4.0 for database creation from a model for now or am I doing something wrong here?
I was able to get this connection string to work with SQL Express on the same tutorial.
<add name="NerdDinners" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=NerdDinners; AttachDbFilename=|DataDirectory|NerdDinners.mdf; Integrated Security=True; User Instance=True" providerName="System.Data.SqlClient" />
Hope this helps someone in the future.
As a side note. If you're struggling to get the database to auto-create, it could well be because the database user you are using hasn't been given sufficient permissions. I was able to simply go the Security of the SQL Instance (not the database) and amend the Securables of the specific login so that it had the permission to Create Any Database (I didn't Grant any additional rights) and it allows EF4 to do its thing.

Resources