Why ADO.NET still connected to SQL Server with wrong user? - c#

I am running Microsoft SQL Server 2014.
I try to connect to SQL Server using ADO.NET in a C# application.
My connection string is:
var myConnection = new SqlConnection("Data
Source=VIRTUAL-ADMIN\\SQLEXPRESS;" +
"Integrated Security=SSPI;" +
"Initial Catalog=StudentManagement;"+
"User id=...;"+
"Password=...");
My Initial Catalog is a right and existed database.
But my username is a wrong username!
The Password I have inserted is also wrong already.
But ADO.NET still connected to my database and I can execute query and do everything with my database.
How can I fix it?

You have provided both Integrated Security=SSPI; as well as username & password so it by default takes integrated Security i.e. windows authentication and thus you are able to connect with wrong credentials as well.
So, either user Integrated Security or your credentials as you do when connecting to SSMS, when it asks for either Windows authentication or SQL Server authentication
From MSDN:-
Integrated Security When false, User ID and Password are specified in
the connection. When true, the current Windows account credentials are
used for authentication.

Related

C# Connection String Without Windows Authentication Mode

I tried a lot but couldn't solve this problem. I want just my connection sql in c# by sql server authentication mode. I don't want windows authentication. When I enable windows authentication mode then my application does not work. What can I do?
This my connection string:
SqlConnection con = new SqlConnection(#"Data Source=myservername;Initial Catalog=mydatabesename;User Id=myuserId; Password=mypassword;Integrated Security=True");
If I change my password in code it works. Also when I delete Integrated security=true then shows me the following error:
Login failed for user
Did you enable the mixed mode authentication?
Did you grant to the user the access to the DB?
you can do it using SQL Server Management Studio (SSMS)
Did you restart SQL services after changing the mixed mode authentication?
Some of the server level properties takes only after a SQL restart , one such is authentication modes.
Start - > View Local Services -> SQL Server(SQLEXPRESS) > restart it
OK guys ı solved when ı add new login also tick the sysadmin new login in sql server and use it on conenction string in c# ı think this error by the user ıd 'sa' ı change new user name and password then it works thnks all people who try help me
You just need to edit your connection string to include the user name and password as below:
Scaffold-DbContext
"Server=DatabaseServer;Database=DbName;Trusted_Connection=false; User Id=ServeruserName; Password=ServerPassword;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DB

Connection string method wrong. Login failed for user

I have a connection string:
public static string str = "Data Source=SAI-7FD4677573D\\SQLEXPRESS;Integrated Security=True;Initial Catalog=Libary;";
But getting an error:
Cannot open database "Libary" requested by the login. The login failed.
Login failed for user 'SAI-7FD4677573D\Administrator'.
How can I write a connection string?
public static string str = "Data Source=SAI-7FD4677573D\\SQLEXPRESS;Integrated Security=False;User Id=SAI-7FD4677573D\Administrator;Initial Catalog=Libary;";
Please help what should I try?
It might be a typo. shouldn't be Library instead of Libary?
SQL Server can support two kinds of user authentication:
Windows based authentication & user/password authentication.
* Windows based authentication (also called "Integrated security)) is using your Windows login as medium of athentication. In this case you shouldn't proveide "User Id" and "Password" fields in the connection string and the "Integrated Security" param must be set to "true".
* Username/Password based. If you want to use explicitely defined username & password to access SQL Server database, "User Id" and "Password" fields in the connection string have to be specified and the "Integrated Security" param must be set to "false".
Bottom line - Your connection string contains both "Integrated security=true" and "User Id" param provided, which is an error. In case you need Windows authentication - remove User Id, otherwise add "Password" param and set "Integrated security" to false
Based on your data source, you seem to be using SQL Server Express. What you will need to do in order to use integrated security is to use SQL Server Management Studio to setup a login for your Windows user account and then create a "user" account for that login on your database.
What user account did you use to install SQL Server Express on your computer? It was probably a local administrator. So, log off, and log back on as that local administrator (or any local administrator or user who is in the "Administrators" group on your PC), and run SQL Server Management Studio (alternatively, you could hold down the shift key, right-click on SQL Server Management Studio, and select "Run as different user", and then enter your local administrator account). Then go to the "Security" > "Logins" section in "Object Explorer", right-click and select "New Login", then click "Search" and enter the local user account.
Next, select "User Mapping" under "Select a page" on the left-hand side, and put a checkmark next to your database in the list. In the database role membership below that, you can configure the base roles that your user will have, if desired.

Domain considerations when using trusted_connection in SQL Server connection string

I have been trying to understand what using trusted_connection=true in a SQL Server connection string (from within C#) means. I understand that it uses the current Windows user credentials to connect to the database. But does it mean the database server and the current user can be in different domains, same domain or in different but trusted domains ?
trusted_connection=true means Integrated Security=SSPI;
If this is not present in connection string then you need to specify userid and password in connection string as:
server=yourservername;database=yourdatabase;user id=YourUserID;password=password
If any of two (Trusted_Connection=true or Integrated Security=true/SSPI) is present , then the Windows credentials of the current user are used to authenticate against SQL Server and any useriD=userid and password=password text will be ignored.
Whichever number of users may present and fromwhichever user you may have logged in, it will ignore the stuff if:
Trusted_Connection=true

Persist Security Info in .Net 4

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application.The application connects to a Microsoft SQL Server 2008 database.
The application uses a Microsoft ADO.NET SQL Server managed provider.When a connection fails, the application logs connection information, including the full connection string.The information is stored as plain text in a .config file.
You need to ensure that the database credentials are secure.
Which connection string should you add to the .config file?
A.Data Source=myServerAddress; Initial Catalog=myDataBase; Integrated Security=SSPI; Persist Security Info=false;
B.Data Source=myServerAddress; Initial Catalog=myDataBase; Integrated Security=SSPI; Persist Security Info=true;
C.Data Source=myServerAddress; Initial Catalog=myDataBase; User Id = myUsername; Password = myPassword; Persist Security Info=false;
D.Data Source=myServerAddress; Initial Catalog=myDataBase; User Id = myUsername; Password = myPassword; Persist Security Info=true;
According to the guide, the answer is 'A'. But in my opinion, the Answer is 'C'. If we are using Integrated Security = SSPI, we don't need to supply UserID and Password. So, Persist Security Info=false has no effect.
As far as I know, Persist Security Info only takes effect if the connection string has User Credentials.
Could you please advise me which one is correct? Thanks.
You are right. Persist Security Info=false has effect only if user name and password provided in connection string. But question is "What should you store in .config file" and considering that "information is stored as plain text" you should not store UID and PWD in config file.
If you store C, PWD and UID can be extracted from .config file. But if you store A, there is no credentials to extract.
I'm not sure, why A has "Persist Security Info=false", but looks like it is a good practice.
See MSDN examples:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/ff647552.aspx

can not open database on localnetworks

I write a win app,and i create my database on the server by codes.now every client on local network can't login to my database and this error occured
:"cannot open database "test" requested by the login.the login failed for user "farzane".
the connectionstring for to make my database is:
ConnectionString=#"Data Source=SERVER\SQLEXPRESS;Initial Catalog=master;Integrated security=SSPI;Persist Security Info=False";
and it's my connection string for open my database:
ConnectionString=#"Data Source=SERVER\SQLEXPRESS;Initial Catalog=test;Integrated security=SSPI;Persist Security Info=False";
how can give permission for logining to my database to any client with codes???
thanks in advance for any help.
I would check two things here:
Ensure that your SQL Express install allows remote connections. (Simple to check using SQL Server Studio Manager).
You are using trusted authentication in your connection string. You have to explicitly give users on your domain access on the database. You will have to this in SQL Server.
are you using a domain for the network ?
if yes then make sure that the user name has access to the SQL server
if you're using a workgroup then it won't work... just create a user on the sql server and use the sql server auth at the server and connection string
Points i concluded:
First of all the users who are going to create the database , must be authorized to use master database. So ask your admin to allow permission to farzanne.
If you(farzanne) are admin, set farzanne to create databases permission to true. Or the other users that might create dbs. Also, if you allow all users then it will be difficult to handle, your application, so be alert.
What is the need of the dynamically createing database from application. Is this a part of setup or deployment or you are creating an isolated space that is different user different database.

Categories

Resources