I have a pipeline that deploys app service, sql server, and sql db in Azure. Deploys the zipped application in app service.
However when I run the following command from Console under Development Tools I get an exception.
Command
dotnet mywebapp.dll
Exception
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'myid'.
I have a sql server with two DBs, below is the code in startup.cs for connection string(.net 6 web app)
services.AddDbContext<MyDBContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MyDBContext"));
options.ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.NavigationBaseIncludeIgnored));
});
services.AddHangfire(options =>
{
options.UseSqlServerStorage(() => new Microsoft.Data.SqlClient.SqlConnection(Configuration.GetConnectionString("MyHangfireContext")), hangfireConfig);
});
hangfireConfig has been defined as a separate variable (SqlServerStorageOptions)
My connection string in appsettings
"MyHangfireContext": "Server=tcp:mydb.database.windows.net,1433;Initial Catalog=HangfireDB;Persist Security Info=False;User ID=myid;Password=mypassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"MyDBContext": "Server=tcp:mydb.database.windows.net,1433;Initial Catalog=MyAppDB;Persist Security Info=False;User ID=myid;Password=mypassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
Also to add, this app setting is an environment specific app setting and I have the ASPNETCORE_ENVIRONMENT defined so it is picked up.
After reading your descriptions, I thought the issue related to the deployment. So please follow my steps to troubleshoot the issue.
Open App Service Editor (Preview)
and check the appsettings.json
file.
Check the connectionstring is correct or not ?
If the connection string is correct, and the issue still occurs, so could you hard code it inside your Program.cs file. To check the issue related to code or environment ?
Please create a temp rule to allow all ip to access the db in Firewall.
You have confirmed the connection string works well in SSMS, then please try to connect it by using vs2022 and get the new connection string, I know we can get it from azure portal, please try it.
Copy the new connection string, and use it in your appsettings.json file or add it in azure portal.
The new connectionstring format is
Data Source=tcp:your_sqlserver.database.windows.net,1433;Initial Catalog=db_name;User ID=jason;Password=********;Connect Timeout=30;Encrypt=True;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False
Tips
Usually our test database link is placed in the appsettings.json file, and there is no need to modify it when deploying. We can add server dependencies when VS2022 is released.
It doesn't matter if you use other methods, we can add it manually in the azure portal, and the configuration in the appsetting.json file will be overwritten after adding.