Can't access database, access denied for user - c#

I got this error message while connecting my app with the database at my website.
If i try using XAMPP using my computer, its work well.
FYI, the username and password is same as username and password that i created using XAMPP.
and also grant the privileges.
this is the connection string. for example the server is 174.125.80.140, the database name is myDB, the Uid is alfred, and the password is Alfred111.
MySqlConnection conn = new MySqlConnection("Server=174.125.80.140; Database=myDB;Uid=alfred;Pwd=Alfred111;");
I'm using MySQL client version: 4.1.22.
I'm still can't access the database. is there any solution??

If you are using MySQL workbench,
1) Start the workbench
2) click on the option "Users and Privileges" under SECURITY
3) click on add user (for the specific user), this is more secure because it lets you handle who has access over your database and lets you control access.
however if you want to grant access a large number of users for an application like c# application, then hard-code the username and password in the application, from the user privileges that you have set above.
Hope this helps (^_^)

Yes, you may have supplied the user and password correctly but have you configure the server (new server) to accept Uid=alfred;Pwd=Alfred111;?
Adding User in MySQL Server

If your app is on a different host that the MySQL server then you most likely need to add a new user granting permission for that host. Your alfred user is probably allowed by localhost and nothing else. Try CREATE USER 'altred#'%' identified by 'password'; and then grant that user privileges on myDB.
You can replace % with a specific IP address or hostname as well, % allows the connection from any host which is not necessarily safe.
You can try the following query to see the allowed user/host combos:
SELECT `User`, `Host` FROM `mysql.user`
Hope that helps.

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.

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

Access Denied Error for user 'root'#'***.***.***.***'

Error: dotnet mysql connection Access denied for user 'root'#'***.***.***.***'
I got this error when I use MySqlConnection to connect to a mysql server. I put in the correct user name and password, and I can use phpmyadmin to log in the server with the username and password.
Also, there is a line with root and % in the privileges page. Is there anything else that I don't know that could make this error?
Better if you show your code.
Try to craete a new user like:
CREATE USER 'test_user'#'localhost' IDENTIFIED BY 'some_pass';
Then grant all PRIVILEGES ON shop:
GRANT ALL PRIVILEGES ON shop.* TO 'test_user'#'localhost';
Try to connect via this user. May be it is due to PRIVILEGES issue.

login failed error for my database on localnetwork

I created my database on the server using code. Now no client on the local network can login to my database. This error occurs:
:"cannot open database "test" requested by the login.
the login failed for user "farzane".
Here is the connection string for my database:
ConnectionString=#"Data Source=SERVER\SQLEXPRESS;Initial Catalog=test;
Integrated security=SSPI;Persist Security Info=False";
How can I fix this problem?
I have about 30 clients on my network, and I don't want to create a login for every one. Is it possible to assign permissions and read/write access for all of them using code?
thanks.
If your clients have existing accounts on your LAN, you can right-click on the database, select Properties and then the Permissions section to assign users and groups the permissions you want them to have on your database. Alternatively, you can go to Security/Logins and right-click the groups or users you want to assign permissions to, select Properties, then choose the User Mappings section to grant permissions on specific databases.
Because you can do it with the GUI, you must be able to do it in code, but that I am afraid is not my area of expertise.

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