SQL Server connection C# : Authentication Windows to database uses computer name instead - c#

I have a problem connecting to my SQL Server Database, i should use a Windows Authentication, so i added Integrated Security = true in my connection string but i get the wrong identity. The program tries to open the connection to the database with the computer name instead of the session name.
I get the error that the login is unknown in the Database, of course it's unknown it's not the good one.
Here is my connection string :
"Server=SERVER\DEV;Database=MYDATABASE;Integrated Security=true"
(I replaced the true names of the server and the Database for confidentiality issues)
Here is the C# code:
private string connectionString = ConfigurationManager.AppSettings["connectionString"];
public IList<string> GetAuthorizedGroupsForAJob(string jobId)
{
IList<string> res = new List<string>();
try
{
SqlConnection cnn = new SqlConnection(connectionString);
//The error happens in the line below
cnn.Open();
I searched for a while but everyone who gets the login error seems to forget the Integrated Security=true or Trusted_Connection=true.
Any help is welcomed here

Enable ASP.NET impersonation on IIS at the website level if you want the logged in user to reach your SQL server.

Related

Why is SQL Server using a different user to create the session?

I am trying to connect to SQL Server database from a .NET Core web application,
here is the connection string I am using:
Server =.; Database = DBNAME; User Id = tb; Password = pass; Trusted_Connection = True; MultipleActiveResultSets = True;.
The previous setup generates an exception:
An error occurred using the connection to database 'DBNAME' on server '.'.
System.Data.SqlClient.SqlException (0x80131904): Error opening session for the user 'MyDomain\ServerMachineName$'.
I think it is trying to connect using another domain account that I don't even see in the list of database users under security tab, nor in the users of the server instance.
In the startup file, the related configuration is:
services.AddDbContext<MyDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
I tried adding different users with the required privileges to the SQL Server instance and to the particular DB, but none of these worked.
Questions:
Why is the system completely ignoring the connection string I am specifying?
Is there a work around to this?
What Trusted_Connection = True; means is ignore the passed in user id and password and instead use the windows credentials of the user running the program. As your program is likely running as a service the "user" it runs as is MyDomain\ServerMachineName$.
Setting trusted connection to false will have it use the userid and password provided in the connection string.

how to deploy winform app with mdf file on server

I have a winform app with a database running on sql server 2012. I want the app to work on multiple computers connected to a server in the local network.
I deployed the app using clickonce and moved the mdf file to the server.
Then I installed the app in a few computers and it works on every one of them separately however when the app runs on one computer and I try to open it on another one I get the following exception:
Cannot open database (database name) requested by the login. The login failed.\r\nLogin failed for user 'USER-PC\user'
using (SqlConnection con = new SqlConnection(conString))
using (SqlCommand command = new SqlCommand("select 1", con))
{
con.Open(); //exception thrown here
object returnValue = command.ExecuteScalar();
if (returnValue != null)
returnString = returnValue.ToString();
con.Close();
}
The connection string is:
Data Source=(LocalDB)\v11.0; AttachDbFilename=path on server\database
name; Integrated Security=true;
I have tried to change attachdbfilename to Initial Catalog=database name, and add users to the db in ssms and add user id and password to the connection string. Nothing helped.
I'm afraid I have some basic concept misunderstood and doing it the wrong way.
How can I resolve this problem?
Please check/correct broken links between Login and DB-User with sp_change_users_login.

SqlException: Cannot open database <database> requested by the login. The login failed

I am developing an application in C# using Visual Studio 2013 and want to connect to my database.
I've attempted to do this the code and connection string below.
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=[Demo Database Nav (5-0)];user=us;password=id";
I get the error message:
System.Web.Services.Protocols.SoapException: Servern kunde inte behandla begäran. ---> System.Data.SqlClient.SqlException: Cannot open database "[Demo Database Nav (5-0)]" requested by the login. The login failed.
Login failed for user 'us'.
I have opened SQL Server and added the login 'us' with the password 'id'. I have made this login a db_owner and associated it with the right Database. I have tried this with several logins but none of them have worked. Is there something wrong with my connection string?
I have developed a lot of C# application that connects to SQL Server 2005/2008 database, and my connection strings are:
Data Source=(local)\SQLEXPRESS; Initial Catalog=<database>; Integrated Security=SSPI
for local databases
Data Source=<ip address>\SQLEXPRESS; Initial Catalog=<database>; Integrated Security=FALSE; User ID=<user>; password=<password>
for remote databases
Just fill all "<>" fields with the correct values.
Create an empty text file on desktop, name it with a .udl extension. Once created, double-click on it and you will see a GUI open up to guide you through to build the connection string. Step through it and click on test connection if successful then open the udl file up in a text editor and copy the connectionstring and paste it in your code
Try this:
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Database=[Demo Database Nav (5-0)];User Id=userhere;Password=passhere";
It seems like the user property doesn't have "Id" like "User Id"
Regards.

How to connect to SQL server via TCP/IP which is server site using C#?

Everyone.
I've got some questions here..
I'm using C# and trying to connect the other computer which is server site.
The server site has been built a large database and it can be accessed by SQL server manager 2012.
All I'm trying is to read the data in that database then convert to the other file.
The other project test in my computer works(self-connection), but
Here's my sqlconnection string:
string sqlstring = #"Data Source=XXX.XXX.XXX.XXX\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=Database; Integrated Security=False; Connect Timeout=30;User ID=sa;Password=XXXXXXX";
Do I need to add "AttachDbFilename" into there?
or even database path?
AttachDbFilename=""C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Databasename.mdf"
Because the results are "sa log in failed" or "it couldn't find the database file"..
Is there any good advice? thank you guys!
If you don't really know what arguments the connection string takes you may want to look into the SqlConnectionStringBuilder.
Here is a short code snippet:
SqlConnectionStringBuilder connString = new SqlConnectionStringBuilder();
connString.UserID="sa";
connString.Password = "XXXX";
connString.DataSource = "XXXX"; //if you need to specify a port write
// the portnumber with a ',' behind the address
connString.InitialCatalouge = "MyDatabaseName";
connString.IntegratedSecurity = false; //set this one true if you want to use windows
//authentification instead of sql authentification
string myconnectionstring = connString.ConnectionString;
It is a bad idea to use the super user, consider making a user that has only the privileges you need for your application to minimize security risks.

Saving to database from WinForm

I'm having trouble to save details about a movie in my database. I know the name giving is not good, but I have just made 1 textbox for input right now, as I am getting desperate. Can anyone tell me what I have done wrong in my code.
If it helps I having the following error which suggests that it can't connect to my localDB but I am not sure.
Error from code :
Code:
private void btnRes_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = ("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
com.CommandText = ("INSERT into movieTable (movieID, movieName, movieLength, movieDescription) VALUES ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"');");
com.ExecuteNonQuery();
sc.Close();
Your local database is not accessible over network. Try the following -
Check whether server is on
Check whether firewall is not blocking the Program
Check whether Named Pipes and TCP/IP is enabled in SQL Configuration manager. To access SQL Configuration Manager, go to Start > SQL Server (Version) > SQL Configuration Manager. Then expand Client Configuration (if it's x64 then there will be 2 item , click that does not have 32 with it).
From the right pane select Named Pipe, Vias and TCP/IP and enable all and restart server.
Make sure you are using correct instance name. MQL Server Enterprise or Standard editions might not have instance names but also can be installed with instance name. See in you SQL configuration manager that you are using the proper instance name.
Then try again.
First of All check the Authentication mode of the Sql Server whether it is in Windows Authentication Mode or SQL Server Authentication Mode
Secondly Check whether the service of the SQL Server is in running state or not
If the SQl Server installation mode is SQl Server Authentication Mode then the connection string used is :
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
If the SQL Server installation mode is Windows Authentication then the connection string is :
Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;

Categories

Resources