How to fix "login failed for user" when manipulating SQL database in Azure from a .NET Core webapp? - sql

I have an SQL Database in Azure and a .Net Core web app in which I would like to manipulate the database using Entity Framework Core.
I believe I have all the model, data and controller classes needed for this, and I have a few sample objects I'd like to see in my database.
My problem is that no matter what I do or how I want to manipulate the database, I always get the error "login failed for user", yet the login info is an admin and I believe I set up everything that is needed.
I see the database in the SQL Server Object Explorer, I can connect to it using SSMS or simply on the Azure Portal, and there seems to be no problem with my connection string. The username&password combo is fine, I've checked it multiple times, my IP is enabled in the firewall of the database, I have no idea what could be the problem, please help me!
Here is the error:

The problem is solved now, it was a very tiny, stupid error:
the connection string I copied from the Azure site was waiting for an escaped field, assuming I won't write my username and password manually, which of course won't be happening in the future, I just wanted to test if the whole stuff worked as this is my first project in this area.
So the Azure connection string looked like this: USER={XY};PASSWORD={ASD} but I left the curly brackets in the string even though I didn't use a variable there.

I test and it works fine. So I am doubt about whether you could run it in local with the connectionstring of Azure sql database.
Open your azure sql server on portal and click your sql database.
Click the Connection string and copy the connectionstring. Filled into your username and password.
Then put it to the connection string in appsettings.json on visual studio.
If you could run on local, then you publish will have no problem.
Also, you could follow this article to learn with asp.net core mvc.

Related

Classic ASP Microsoft SQL Server Native Client 11.0 error '80040e14' Login failed for user, but not the one I specified

Getting a very odd error that I can't seem to track down. Running Classic ASP on IIS10. I'm specifying a user in my connection string, but the application seems to be "defaulting" to another user. I can't figure out where this user is being specified.
set conn = Server.CreateObject("ADODB.Connection")
connectionString = "Provider=SQLNCLI11;Data Source=serverxy;Initial Catalog=hazard;USER Id=xxxxx;PASSWORD=yyyyy;"
conn.Open connectionString
...do sql queries, etc...
This happens on both my production server, development server and my development desktop machine.
SQL Profiler helped me figure out how to fix the problem. It showed that the user I was specifying was actually working. There was a view calling a query on another server. The database on this server was actually offline and had been moved. I updated the view to use the new server and all worked after. Completely forgot about Profiler as it's been ages since I've had to use it.

SQL Server Security Permissions issue

I have a strange issue when trying login to my WPF application I published. I am using Microsoft SQL Server 2016, I am also a Server admin. I have a Database called Project Tracking in the server. When trying to log in I am able to login just fine. When another user tries to login they get this error: The underlying provider could not be opened.
Now, if I add them as system admins to the SQL server they are able to login just fine no errors. Although this is not what I want to do for obvious security reasons. How do I go about actually adding them for read/write access to the database?
The connection string in the application is: "metadata=res:///Model.ProjectTracking.csdl|res:///Model.ProjectTracking.ssdl|res://*/Model.ProjectTracking.msl;provider=System.Data.SqlClient;provider connection string="data source=EPLANDB\PROD4W;initial catalog=ProjectTrackingDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
This is my first go around using SSMS and SQL Servers, so not sure what exactly the problem is or how to word it correctly, I hope I gave enough information if someone could point me the right direction.
Here is a picture of my setup on SSMS.
I figured it out. I found this article over the subject and it worked for me.
I had to go into Security for the Server right click Logins and hit New -> Login.
Your are then brought to this screen. Click Search...
Not sure if this part matters. But I clicked Object Types and checked the Groups.
Add the username you want to include. Make sure to include the domain name.
Click User Mapping.
Check Your Database.
Make sure to check data reader and data writer and any other roles you would like to include.
Here is where I found the solution.
Answer to Problem

Connection Error with SQL Azure and Entity Framework on Azure Website

Not sure where to start, but whenever I publish my ASP.NET website to Azure, any pages which have database access give me a message saying "Error. An Error occurred while processing your request." I open up the remote debugger (which is fickle because it refuses to attach half of the time) and I see the error occurs when establishing when trying to access Entity Framework. The error varies between a "network-related or instance-specific" error, or a "Login Failed" error (which could be the result of the previous error, I really don't know).
The ADO.NET connection string SQL Azure gives is
Server=tcp:[servername].database.windows.net,1433;Database=EnsembleMusicWebDatabase;User ID=user#[servername];Password=(password);Trusted_Connection=False;Connection Timeout=30;
But every implementation (inserting that into EF metadata string, changing the server to data source...etc) still gives me the same login error
I'm pretty sure it's a problem with the connection string, but the infuriating part is that I've tried every possible combination I can think of (entity framework metadata, using the SQL Azure database ADO.NET connection strings in any possible way, changing the Azure website connection strings under the Config tab, using just a plain connection string...etc).
I've deleted and rebuilt the Entity Framework models at least 5 times, and every time I can successfully establish a connection to the server and it successfully reads my database and creates the correct models. I deploy the application to localhost and it works. The problem is when I publish, it cannot access the database and keeps giving me these login failed errors (the login details are the exact same as when I set up the EF model).
I think it might be something to do with the firewall, since I can access the DB locally with an approved firewall IP on the server config, but the website itself can't access the database (I have the enable azure services box ticked as well). I'm really at a loss for what to do now, because I just want the site (not any user, just the application) to fetch some data from the database and display it on the page, but I don't understand how this could be so complicated.
Can anyone point me in the right direction? I tried every tutorial and example on msdn and I can't find any solutions on SO that work.
Thanks,
Shaun
I realised that I somehow got into a complete mess with connection strings all over the place and the best way to fix it was just to start again. I deleted my Azure website and database instances, built the database first (created a correct login as well) and then when creating a new website Azure gave me the option to include the database I had just created. I now have a correct connection string that Azure generated, but to be safe (because the metadata made connection strings confusing and I didn't want to risk having this same issue again) I'm not using Entity Framework and just using normal SqlClient queries, since the website only requests two objects from a database.
I think now I've got a correct and working string I can look at it and really understand where I went wrong and how to avoid this if I do end up using Entity Framework.

Cannot make a connection to a SQL Server data provider using the Entity Data Model Wizard

Ok, I never got the this error before...
Unable to find the requested .net framework data provider. it may not be installed.
I'm simply trying to connect to a good old MS SQL Server and I get the above error.
The project is just a Class Library with just a Properties and References folder. I added an app.config file to it.
Here are my steps for my project:
Add New Item
ADO.Net Entity Data Model
EF Designer from database
New Connection...
Data source: Microsoft SQL Server (the Data provider then shows up as: .Net Framework Data
Provider for SQL Server
Continue
Right here, if I click "Test Connection" without any info entered about the server, I get "Test Connection succeeded". I don't know why...
Enter the Server name
Enter the userid and password for SQL Server Authentication
When I select "Select or enter a database name", it tries to load a database list, but it immediately goes blank. I even try typing in the database name
Hit OK
That's when I get the error.
Can someone please tell me what I need to do to resolve this?
Solution solved at this link with the msg posted by Barrett.
http://social.msdn.microsoft.com/Forums/en-US/7b4f353b-77fd-427c-976b-5968abc88c13/visual-studio-2010-unable-to-find-the-requested-net-framework-data-provider-for-sql?forum=vseditor
I hope this helps anybody else out....

How to deploy windows form application with entity framework

I am busy developing my own application, it uses a sql server database and it is connected through an entity framework. I use store procedures to insert, update, delete, select from my database.
The app works perfect on my machine even when I publish it. But my problem comes in when I try to install the app to my friend’s computer. It crashes and does not start because it cannot connect to the database.
Is there a way to publish my app with the database, without importing all the tables, store procedures and database into my friend’s pc? I just want to make it so the user just has to install the app and it works.
Your app relies on the db to work, so if you want to put it on your friends PC then you need to make the DB available somewhere, whether it be a local copy or a copy stored on a server somewhere running SQL.
How are you storing your connection string? Is it hard coded in the app or are you utilising the app.config file? To do what you're trying to do you'll need to put the connection string into the app.config file, so you can change it depending on the installation.
either that or
if you want to run your app without data, put a demo flag or something into the app.config file. Put some code into your app to check this value, if it's true then bypass the SQL code and maybe supply some demo data which is hard coded.
Does this make sense?
You could use SQL CE, but you may find it a little more difficult to 'design' your database in it if you are more used to working in SQL Server.
Have you considered SQL Server Express as an option?
On the connection string issue, you can now get the data connection wizard that Microsoft use in Visual Studio via Nuget; this makes adding a way to dynamically configure connection strings on your clients machine much easier.
Lastly, connection strings for the entity framework are different from standard SQL connection strings. Make sure that you clearly understand the differences before you start trying to configure them programmatically. Julie Lerman's excellent book on the Entity Framework explains the differences well.