MySQLException Access Denied When Using Allow User Variables = TRUE - c#

The DB Code that I've written requires that I use Allow User Variables in my connection string. Without this setting, I am to login to the DB; with this setting, I am unable to login.
Here is an example of the code:
new MySqlConnection:
("server=XX.XX.XX.XX;
Port=3306;
database=store;
UserID=XXX;
password=XXX;
Allow User Variables=TRUE");
I have changed the access privileges for the user, but this has no effect on my ability to connect to the DB. Can anyone please let me know what I'm doing incorrectly? One other note, the DB is hosted on a different server than where this code is running.

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.

Error: the underlying provider failed on open. system.data.sqlclient.sqlexception login failed for user

After deploying my C# win app in client computer, the following error occurs:
The underlying provider failed on open.system.data.sqlclient.sqlexception
login failed for user. cannot open database "EmdadKhordo" Requested by the login
This is my connection string:
metadata=res://*/Models.EmdadKhodroDB.csdl|res://*/Models.EmdadKhodroDB.ssdl|res://*/Models.EmdadKhodroDB.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=EmdadKhodroDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
I use Entity Framework to access a SQL Server database.
Based on the cannot access database error message, authentication succeeded to the SQL instance but the specified database cannot be accessed. This may be because the EmdadKhodroDB database does not exist, is not online, or (most likely) inaccessible under the current security context. Make sure the account you are using has access to the database and the needed object permissions.
The DCL below will add the user to the database. You will also need to grant object level permissions directly to the user or a role of which the user belongs.
USE EmdadKhodroDB;
CREATE USER [YourDomain\YourAccount];
You can also manage your Windows user rights using SQL Management Studio. Just follow instructions from here for creating a new login and also configure mappings to each database you need access to.
Basically you need to make sure that your login is properly defined (Security -> Logins) and that it has the appropriate rights on EmdadKhordo database (read, write etc.). The recommendation is to allow the minimum required rights (e.g. do not allow to alter tables, if only SELECT, INSERT, UPDATE, DELETE statements are performed by the user).

Can't access database, access denied for user

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.

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.

Why does the "drop user" command for SQL not work in C#?

Why does the drop user a_2 command not work when I try to use it in C#, while when I tried it in SQL Server Management Studio, it works there !!???
This is the code that I used it when I create the user & give it a permission:
use DataBaseName;
create login a_2 with password='Aa123';
create user a_2 for login a_2;
grant insert to a_2;
..........
The connection string used :
Data Source=TheServerName;Integrated Security=True;database=master
Note:
I tried to drop a database from C# and it works perfectly without any problem, but when I use it to drop a user the problem raised here !!!
This is the exception that gets raised
Exception is: Cannot drop the user
'a_2', because it does not exist or
you do not have permission.
I believe your problem is that "you do not have permission". Your application is running as a different user from you. You can solve this by either giving the default application user the necessary permissions on the database (not a good idea) or by creating a special user in the database for your application and having the application run as that user.
Be aware that giving your application (or any user, for that matter) permissions to drop users could be very risky if someone figures out how to hack your application. And you aren't actually including the ability to drop the database in your app, are you? Think about it. Instead of creating users in the database, consider creating a Users table just for your application. Then your users won't have such extensive rights to your actual database.
DROP USER would require use DataBaseName;
Or a different connection string
Data Source=TheServerName;Integrated Security=True;database=DataBaseName
That is, the DROP USER only makes sense in the context of your database. When you run it later you are in the context of master = fail.

Categories

Resources