Connecting PostgreSQL with ASP.NET MVC using Npgsql - c#

I'm attempting to connect my database to my ASP.NET MVC application but every time I start the application, I get an error saying that my password is incorrect which is impossible. Please let me know if you can see any mistake I've made in my code.
web.config file
Error
The first error it shows is:
System.Data.Entity.Core.ProviderIncompatibleException:
'An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.
See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.
And the second
PostgresException: 28P01: password authentication failed for user "u173082"

I think your property for the User Id is incorrect. I pulled a functional connection string, which looks like this:
Host=<>;Username=<>;Password=<>;Database=postgres;Port=5432
So, where you have "User Id" in your connection string, try using "Username."

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

MongoDB C# unable to connect to database: Unable to create an authenticator

While setting up my new environment with a freshly deployed MongoDB container with authentication enabled, I ran into this exception: "An unhandled exception has occurred while executing the request. MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.NotSupportedException: Unable to create an authenticator."
In my case I'm using a connection string like this example: mongodb://USER:PASSWORD#HOST:27017/?authMechanism=DEFAULT. This string works perfectly fine in MongoDB Compass but not inside my .NET 6.0 application.
If you take a look at the source code of C# MongoDB driver in MongoCredential.cs#L469, you see this exception gets thrown while checking the auth mechanism.
After specifying the exact auth mechanism in the connection string, all exceptions are gone!
example: mongodb://USER:PASSWORD#HOST:27017/?authMechanism=SCRAM-SHA-256
Hope anyone else googeling around will find my answer helpful!
happy coding.
For me this just changed the exception to unable to authenticate using sasl protocol mechanism scram-sha-256
After some trial and error I got it working by changing the connection string to:
mongodb://USER:PASSWORD#HOST:27017/?authSource=admin
I have no idea why this was needed as the default and SCRAM-SHA-256 connectionstrings works fine in other contexts...
Remove the authMechanism=DEFAULT parameter from the connection string

Fatal NServiceBus exception when starting endpoint, cannot open database

We just made some changes to our project and deployed it. We have 7 endpoint databases in our SQL Server database for NServiceBus. We recently added 2 more. One of the two services using one of the two new endpoints fails to start. When looking at our log file we see the following error:
FATAL NServiceBus.GenericHost [(null)] - Exception when starting endpoint.
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "A_NSBEP_WebEventProcessor" requested by the login. The login failed. Login failed for user 'xxxx'
The connection string is
'connection.connection_string'='Data Source=localhost\DevSQLServer;Initial Catalog=A_NSBEP_WebEventProcessor;Integrated Security=true;Enlist=true'
All of the connection strings are configured in OctopusDeploy. They are all identical except obviously for the database name. All of the other services start and login to their respective endpoint with no problem. I even went through the process of creating a .udl file, connecting to the database and using that slightly different connection string. Still without success.
Any thoughts?

Connect Azure SQL Entity Framework 7

I'm trying to use a Azure SQL instance and use code first migrations to create the database.
When I use without run migrations it throws an error:
Invalid object name 'MyObject'
And I can see in the exception details my connectionID, because of this I believe that it could handle the connection.
But when I try to use migrations database update to create the object, I can't connect when I use myContext.Database.EnsureCreated(); I can't see the connection id and the exception is
can't connect to the server
I already created a firewall rule to my ip address and it didn't worked.
Could anyone help me?
Thanks in advance.
I figure it out.
The string connection was wrong, i copied from the VS SqlExplorer, i thought it was correct but that wasn't the true.
I see on the manager azure old portal examples for string connection using tcp indicating the port.

Deploying ASP.NET membership to Godaddy

I'm trying to deploy a website using ASP.NET membership and the hosting company is godaddy. Problem is that for some reason an error is being thrown when I log in. I've modified some pages for testing purposes to see if I can pull data from the database and it works fine. So I know it's mapping to the proper source. The error I'm receiving is the following:
"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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
Any help would be much appreciated.
It sounds like something is using the LocalSqlServer automagic connection string still. I'd add<remove name="LocalSqlServer /> to your <connectionStrings> and see what blows up.
When you setup membership it normally adds another connection string to your web.config. I think it creates a key in the web.config called ApplicationServices that contains the connection string that the membership classes will use (I think this depends on what membership provider you are using). When you checked the connectionstring did you also check that one?
When you use membership asp.net creates a SQL Database called ASPNETDB.MDF in the App_Data folder which you will need to ensure is set up correctly on the host as you would have done with your main database.
You'll also need to amend the additional connection string in your web.config file so that it points to this database.
Membership settings in web.config can be configured to use any connection string that is defined in web.config. If your membership tables are in the same database as the rest of your tables, I imagine you shpould be able to use a single connection string for everything. That is what I have been trying to set up on my own GoDaddy-hosted website lately, and that is what reading MSDN's documentation suggests should be possible. The "membership" section of "system.web" has a "providers" section, and "add" for that section has a "connectionStringName" attribute.

Categories