Why do I get an SqlClientPermission exception? - c#

I have built a C# WinForms application which accesses a SQL Server 2008 for data manipulation.
The application works fine on the machine that was developed on. However when I copy the EXEs and move it to another computer (Windows XP), I got the following error during the login to the system (in other words, when i access the database).
Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed
Here's my connection string:
Data Source=vmsql;Integrated Security=SSPI;Initial Catalog=ChequeBookInventory
Stack trace is given in a screen shot
Any idea how to fix this issue?!

Most likely, the program is not executed with full trust. Either because the computer is configured like this or you are running it from a network share...
Some more info can be found here.

Grant the executing user rights to the database.

Well It was a combination of things that did the trick...
Setup code level access... I had allow full trust on machine level that network share URL through .NET Fx configuration (Whc i had to install seperately)
Install .NET latest version.. (I stuck to 3.5 ;-] )
Thanks for all the help guys :-)!!

If you want to connect to SQL Server remotely, and don't want use the username and password but Windows Authentication, that's just one way - "AD" ,I think AD might cause more problems (but AD is more secure), and I am really sorry about that my answer is not correct, I found some references about that issue,I think it might be help.
1.How to troubleshoot the "Cannot generate SSPI context" error message
2.“Cannot Generate SSPI Context” error message, more comments for SQL Server
3.“Cannot generate SSPI context” error message, when connect to local SQL Server outside domain
4.Service Principal Names (Windows)
5."Access Denied" error message when you try to access remote resources
that's bad answer bellow:
If you use "Integrated Security=SSPI", that means you can't connect to SQL Server remotely.
if you want to connect remotely, please use "User Id=myUsername;Password=myPassword;" instead of "Integrated Security=SSPI".
(Of course your SQL Server must accept "remote connection")connection string for connecting to data sources

Related

IIS seems to not being able to connect using Entity framework provider

I've being trying in vain to solve a problem on a newly created windows server with IIS hosting an ASP.NET application that was copied from another environment. The issue is that I cannot manage to connect to the database when the application tries to open a connection via entity framework.
If this is off topic for SO let me know so I can move to another channel, I thought it belonged here as seems specific to EF.
In detail, I have the following connection strings:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz" providerName="System.Data.SqlClient"/>
<add name="MembershipConnection" connectionString="metadata=res://*/App_Code.Membership.Membership.csdl|res://*/App_Code.Membership.Membership.ssdl|res://*/App_Code.Membership.Membership.msl;provider=System.Data.SqlClient;provider connection string="Data Source=LOCALCOMPUTER\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=yyy;Password=zzz;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient"/>
...
xxx, yyy, zzz of course have the proper values. LOCALCOMPUTER is the FDQN of the local computer, which is not on a domain
The first connection string is for the Microsoft login library, while the second uses entity framework from our code.
When I browse to the site, I can successfully access the login form, and if I try to insert a wrong password i get a message saying so, meaning the application managed to check the database using the first connection string, but when i insert a correct one, i land on an exception as soon as the code tries to create a context.
The underlying provider failed on Open.:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - The wait operation timed out.)
In the event viewer it tells me that the user has been authenticated via Authentication Type: Forms , which I assume it means the auth system (the one using ApplicationServices connection string) is working, the stack trace confirms that the error occours when I try to use EF in code, meaning the DB has been used succesfully to check user credentials
note that:
The web app works in other environments
I can connect to the sql server using the credentials via SSMS
I've assigned a local windows account to the app pool that can also access the database, and tried changing to Integrated Security=SSPI
Both .Net 3.5 and .Net 4.8 are installed
The database is on the local machine and has been restored from a dump of the other one, its users have been mapped, and to rule out issues i gave it ample authorisations with dbowner datawriter and datareader
I've been trying to fix this since a couple days checking similar issues on SO with no avail and I don't really know how to proceed, is there something I can do to troubleshoot the issue?
Apologies, I've found out the answer, and it's specific to the code base. Apparently the connection string was being ignored (despite being perfectly usable) in favour of data saved in a table.
Had to contact the previous developer to find out, guess it's a good example of how important is to document your code
I'll close the thread, thanks for all who tried to help

Access denied when connecting to an AWS MySql database from an ASP.NET application

I've just set up a MySQL database in AWS, however every time I try to connect programmatically I get an access denied exception. Connecting through MySQL Workbench works absolutely fine.
I've added my IP address to the Inbound Rules in AWS (without it, Workbench can't connect so it's definitely set up somewhat correctly) and even added a rule to allow all IP addresses for all traffic:
This is a bare minimum example of what I'm trying to do:
using MySql.Data.MySqlClient;
MySqlConnection connection = new(CONNECTION_STRING);
connection.Open();
which throws the exception on connection.Open().
Connection string:
Server=aa11sgbkk911b5j.cevakqici96g.eu-west-2.rds.amazonaws.com;Database=ebdb;Uid=username;Pwd=password;
The exact exception:
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Authentication to host 'aa11sgbkk911b5j.cevakqici96g.eu-west-2.rds.amazonaws.com' for user 'username' using method 'mysql_native_password' failed with message: Access denied for user 'burntorangeadmin'#'cpc150000-brnt4-2-0-cust812.5-2.cable.virginm.net' (using password: YES)
Source=MySql.Data
...
Does anyone have any idea why I can't connect?
I originally had the database coupled to an Elastic Beanstalk instance, and after creating a new one separately everything worked as expected.
So still no actual solution to why it wasn't working in the first place, but decoupling has done the trick.
Check that you have granted the appropriate privileges to the user account
Eg.
CREATE USER 'cdcuser'#'%' IDENTIFIED WITH mysql_native_password BY "cdcpwd";
GRANT ALL PRIVILEGES ON *.* TO 'cdcuser'#'%' ;
FLUSH PRIVILEGES;

Security Exception ASP.NET/MySQL

i am having trouble with the Connection String in C#
the database is on the server via phpmyadmin(i do not own the server its online)
got hosting via GoDaddy as well as GlobeHost but with both same problem
my current connection srting is string strcon = "Server=ipAddress(OfServer);Database=testing;Uid=myusername;Pwd=mypassword";
and i am getting this error while Connection.Open();
check the link testing page
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=**************' failed.
Your web host has configured the server to block outbound connections, at least specifically on your website and possibly that port number.
Speak to your web host to find out if you can be granted permission or if other ports are unblocked (though this is a Code Access Security configuration, so I doubt it depends on the port setting at all) - if not then you will not able to use an external database at all.
Note that using an off-site DB will generally result in very poor performance because of network latency. Is there a reason you're not using a DB provided by your host?
Connection String format is wrong. It has to be
connectionString="Server=192.168.0.XX;Database=DBname;User Id=bdusername;Password=dbpassword;"

SQL Request connection string error

I have written a WCF service with database connection in Visual Studio 2012. When I deploy the database on my server I get an error:
The server encountered an error processing the request. The exception
message is 'Instance failure.'. See server logs for more details.
I followed the steps from here to help me but it only helped me to an extent.
My connection string seems to be the problem. My connection string is
Data Source=.\\MSSQLSERVER;Database=jarvis;Integrated Security=SSPI;User Id=sa;Password=123
The server that I am using is running SQL Server 2012 Developer edition. What courses this problem and how can I fix this?
Your connection string is currently using Integrated Security=SSPI and also providing a user and password. You should be only using one of those. Integrated Security means that you use the windows authentication method, so it impersonates the windows user and tries to log in with it (so you shouldn't also provide a user and password). On the other hand, you can use the User Id and password, but without Integrated Security.
So, it's either:
Data Source=.\\MSSQLSERVER;Database=jarvis;Integrated Security=SSPI
Or:
Data Source=.\\MSSQLSERVER;Database=jarvis;User Id=sa;Password=123
Trial and error just taught me that the problem was "Data Source=.\MSSQLSERVER;" - It's the double \. That's an escape sequence in C#. Everything worked when I switched the connection string to .\MSSQLSERVER.

"Can not find server or is not available" on test server but not while debugging

I keep getting an error when trying to access the database from a web site it returns an error that it can't find the server or it's inaccessible.
Now I did a search and I found many possible reasons - I turned on named pipes and also did what this link suggested (i.e. set the IP addresses to "enabled") yet I'm still getting the error.
Error SqlException: System.Data.SqlClient.SqlException (0x80131904):
Error An error has occurred with the network or the occurrence when
connecting to SQL Server. Can not find server or is not available.
Verify that the instance name is correct and that the configuration of
SQL Server allow remote connections. (provider: Named Pipes Provider,
error: 40 - Could not open a connection to SQL Server) at
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) at
I also downloaded this little app and I copy/pasted the connection string from the web.config used in IIS for this site and the app is able to connect and query the same database being targeted by the web site.
And, as the title says, this error is utterly missing when I debug it in VS2010. What could be causing this error when launching from the server?
Edit: the connection string includes a user name and password (it's not Windows Authentication).
Most likely a security issue. If using windows authentication then make sure the thread pool that the web application is running as has sufficient DB access\privileges.
The issue is that the web.config references another config file
<appSettings file="config/dev.config">
and so the connection string in web.config is ignored over the one in dev.config - an error to have it in both (the one on the test server was created by someone else and worked on static pages - it only failed when the website had to query the database), that's why the version in Visual Studio was running fine.

Categories