C# Connection String Without Windows Authentication Mode - c#

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

Related

How to avoid connections against empty/null database by Entity Framework

We use entity framework to read from an existing database.
This is a simplified version of our code.
using (my context context = new mycontext())
{
if(context.Database.Connection.State == System.Data.ConnectionState.Closed)
{
_logger.Info(" Opening the connection to the database");
context.Database.Connection.Open();
}
context.Configuration.LazyLoadingEnabled = false;
IQueryable<mymodel> people;
people = context.People.OrderBy(x => x.Firstname);
_lstContacts = people.ToList();
if (context.Database.Connection.State != System.Data.ConnectionState.Closed)
{
context.Database.Connection.Close();
context.Database.Connection.Dispose();
_logger.Info(" Connection to the database Closed");
}
}
It works 100% of the time, but...
On our UAT environment we can see failed connections to the Microsoft SQL server with the error:
Login failed for user "my user". Reason: Failed to open the explicitly
specified database "null". Client my IP.
For us, these are ghost connections because at the time when we see the errors in the SQL server, our code is not executed.
Initially we didn't close and open the connection explicitly, we just added it trying to control when EF open and closes the connection, but it didn't fix the issue.
Our connection string is using the following format:
<add name="MYCN" connectionString="metadata=res://*/CVs.Cvs.csdl|res://*/CVs.Cvs.ssdl|res://*/CVs.Cvs.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver\;initial catalog=mydatabase;Integrated Security=;User ID=myuser;Password=XXXXXXX;MultipleActiveResultSets=True;App=EntityFramework"/>
As you can see, we are specifying the database in the connection string and our user only have access to our database, so we understand the error when EF doesn't include the database in the connection string, but we don't understand why it's trying to perform these connections.
We know the connections are coming from our application, because we are the only one using that specific user, the IP is the IP of our server, and because the logs in SQL server tell us that the application is "EntityFramewrok"
I didn't personally see the error before, but researched for you and seen that many people suffered from the same problem discussed here: https://blogs.msdn.microsoft.com/sql_protocols/2006/02/21/understanding-login-failed-error-18456-error-messages-in-sql-server-2005/
I read all the messages in the website specified, and here are the solutions offered and at least one other user confirmed that it worked. You might not use 2005 as you didn't specify your version in your question, some solutions I believe will still work for you. Try the list below.
Solution list:
1) Please check the state number of this error and search solution by the state number in addition to the message, might give your more accurate solution proposals. Most common states are listed:
All state-error descriptions you can find here: https://sqlblog.org/2011/01/14/troubleshooting-error-18456
2) Make sure the username and password are correct.
3)
Logon to SQL Server using windows authentication.
Right click in the query window that appears and select "Open Server in Object Explorer"
Go to object explorer and open the "Security" folder and then the "Logins" folder.
Double-click the "sa" user and change the password. Also, like another user mentioned above, untick the "Enforce Password Policy" in
addition to resetting the password.
4) Try to change the password and turn off the policy, and try with new password.
exec sp_password #new = ‘sqlpassword’, #loginame = ‘sa’
alter login sa
with password = ‘sqlpassword’ unlock,
check_policy = off,
check_expiration = off
5) Run your application/browser and SSMS (if you work on it) in administration mode.
6)
Open Windows Services
Configure Microsoft Single Sign-on Service to use the proper account
Open Central Administration >> Operations >> Manage settings for single sign-on
Configure properties to use the same account used for Microsoft ‘Single Sign-on Service
7) Go to Sql server configuration manager and Enable TCP/IP and named pipes
8)
go to sql server
right click on server, choose properties
click on security
on server authentication, enable SQL Server authentication
These might help:
https://www.wikitechy.com/errors-and-fixes/sql/login-failed-error-18456-severity-14-state-38-reason-failed-to-open-the-explicitly-specified-database
https://dba.stackexchange.com/questions/90445/login-failed-for-user-error-18456-severity-14-state-38
I think this is just an access issue for myuser in the UAT environment. Just make sure myuser has access in the UAT environment for UAT database and you should be good.

Connect c# to a SQL Server in different domain

I have an c# app that tries to connect to a SQL Server that is in the same network but out of domain. I'm trying to use SqlConnection (I would prefer not use ODBC or ole db).
My code is the follow:
con.ConnectionString =
"Server=PCX\\SQL;"+
"Initial Catalog=BBDD_SinGuid;"+
"User id=\\\\PCX\\user;"+
"Password=passwordofuser;";
And I'm sure that the user and the password are correct and are allow to connect to the SQL Server. The error that throws is a fail in the login with the user \PCX\user.
I'm missing something?
you need to add trusted connectioon here..
Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=yes;
for more information you check below link it's has lot's of suggestion may be you will get your answer.
http://www.connectionstrings.com/sql-server-2008/

How to change SQL Server 2005 from Windows Authentication to SQL Authentication?

I have installed SQL Server 2005 in Windows Authentication, now I want to change it to SQL Authentication.
However, I cannot reinstall SQL SERVER and Management again, because it will lose the data in it.
Also I want to know what changes do I need to make in my Connection class to open connection, as my current is
public SqlConnection con= new sqlConnection("server=.\\SQLEXPRESS;database=Restaurant;integrated security=sspi");
Just need to ask one more thing, I can see my database in WIndows Auth and in SQL auth, so how can i change, so that any other user need to login before it peeps into my database. thanks
Using SQL Server Management Studio
*To change security authentication mode*
In SQL Server Management Studio Object Explorer, right-click the server, and then click Properties.
On the Security page, under Server authentication, select the new server authentication mode, and then click OK.
In the SQL Server Management Studio dialog box, click OK to acknowledge the requirement to restart SQL Server.
In Object Explorer, right-click your server, and then click Restart. If SQL Server Agent is running, it must also be restarted.
To enable the sa login
In Object Explorer, expand Security, expand Logins, right-click sa, and then click Properties.
On the General page, you might have to create and confirm a password for the login.
On the Status page, in the Login section, click Enabled, and then click OK.
Using Transact-SQL
To enable the sa login
In Object Explorer, connect to an instance of Database Engine.
On the Standard bar, click New Query.
Copy and paste the following example into the query window and click Execute. The following example enables the sa login and sets a new password.
ALTER LOGIN sa ENABLE ;
GO
ALTER LOGIN sa WITH PASSWORD = '<enterStrongPasswordHere>' ;
GO
You can pass below sample(change appropriate UID & PWD) connection string in your connection object
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
To use SQL Server authentication, change the line:
public SqlConnection con= new sqlConnection("server=.\\SQLEXPRESS;database=Restaurant;integrated security=sspi");
to
public SqlConnection con= new sqlConnection("server=.\\SQLEXPRESS;database=Restaurant;user id=#USERID#;password=#PASSWORD#;Trusted_Connection=False");
Where "#USERID#" and #PASSWORD# are the specific User ID and Password respectively
To change the server security type:
You just need open SQL Server Management Object Explorer, right click the server, go in Properties, Security page, under server authentication, select the mode you want.
To modify the connection string:
1. You need to have a SQL user first. Still in the SQL Server Management Object Explorer, right click on user which under Security node, click new, then you can create one.
2. add the "User ID" and "Password" segment to the connection string.

Login failed for user. Trying to access a SQL Server Express database

I'm trying to access my SQL Server Express database, but every time I try, I receive this message:
Login failed for this user.
Here is the code:
sqlConnection cs = sqlConnection("Data Source=.\\SQLEXPRESS;
Initial Catalog=MyDatabase#4; Integrated Security=TRUE");
cs.Open();
MessageBox.Show(cs.State.ToString());
cs.Close();
How can I fix this problem?
Try to write your computer name instead of . after "Data Source". For example:
Data Source=Michael-PC\\SQLEXPRESS;Initial Catalog=MyDatabase#4;Integrated Security=True";
The SQL is not able to authenticate your windows user because it may have no access rights on the database... Try using an administrator account or to use a SQL Server privileged account.
What I sometimes do is get Visual Studio to connect, via the Server Explorer, and then copy and paste the connection string from there. If you have SQL Management Studio Express, that will work too.

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