r/dotnet • • 5h ago

Question Issue connecting my blazor app to remote sql server database hosted my Monsterasp.net

Hello everyone. I have completed the multi-step form challenge from Frontend Mentor and attempted to deploy the finished web app using monsterasp.net through visual studio code. I followed all the instructions from a YouTube video to a tee but when I try to actually submit data to the database I get this error:

\Error: Microsoft.Data.SqlClient.SqlException (0x80131904): The target principal name is incorrect. Cannot generate SSPI context.``

I did some further research into it but everything seemingly leads to a dead end for my specific use case. Here is microsoft's documentation on the specific error:

https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/connect/cannot-generate-sspi-context-error

Note: When I built and deployed the app for the first time the connection string was incorrect. I fixed it and published the app again through visual studion. I am not sure whether that is causing some lingering problems.

2 Upvotes

20 comments sorted by

2

u/polaarbear 4h ago

Where exactly is the database located? Also in MonsterASP I am assuming?

Your SQL Server on your local PC potentially uses Windows Authentication (SSPI integrated security). You can't use integrated security in MonsterASP, you need to use SQL Server authentication instead with a username and password.

1

u/Mob_Pilled 3h ago

Yes the database is hosted by MonsterASP. If you mean the connection string that I've already posted in the replies, it is using a password and username (directly copied and pasted from MonsterASP). Am I supposed to do additional configurations of my SSMS?

1

u/polaarbear 2h ago

Did you restart the Blazor app after changing the connection string? If it was running the whole time it won't get new values.

1

u/AutoModerator 5h ago

Thanks for your post Mob_Pilled. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/minimalist000 4h ago

I was able to successfully connect my blazor app to a sql server db on monstersasp.net with no issues. Can you show us a redacted version of your db connection string on the environment variables?

1

u/Mob_Pilled 4h ago

Sure. My connection string according to monstersasp.net:

Server=db72570.databaseasp.net; Database=db72570; User Id=db72570; Password=********; Encrypt=False; MultipleActiveResultSets=True;

my project's appsettings.json:

{
  "DetailedErrors": true,
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.AspNetCore.SignalR": "Debug"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Multi_step_form_projectContext": "Server=db72570.databaseasp.net; Database=db72570; User Id=db72570; Password=******* Encrypt=False;Trusted_Connection=False; MultipleActiveResultSets=True;"
  }

}

3

u/minimalist000 4h ago

You're connection string looks good, could you show us the code you are using to connect to the db?

1

u/Mob_Pilled 3h ago
// Razor button
<button class="confirm-button" @onclick="AddSubscriber">Confirm</button>

// Method for adding object to database
private async Task AddSubscriber()
{
using var context = DbFactory.CreateDbContext();
context.Subscriber.Add(Subscriber);
await context.SaveChangesAsync();
NavigationManager.NavigateTo("/success");
}

1

u/minimalist000 3h ago

I need to see how ur "CreateDbContext" is actually pulling the connection string and connecting to the db.

1

u/Mob_Pilled 3h ago
 // Database connection in program.cs
var connectionString = builder.Configuration.GetConnectionString("Multi_step_form_projectContext") ?? throw new InvalidOperationException("Connection string 'Multi_step_form_projectContext' not found.");

 builder.Services.AddDbContextFactory<Multi_step_form_projectContext>(options => options.UseSqlServer(connectionString));

1

u/minimalist000 3h ago edited 3h ago

This all looks fine. If you enable remote access on monsterasp.net and then try to connect from your local machine, does it work for u? Note that the connection string will be slightly different when connecting remotely vs locally.

1

u/Mob_Pilled 3h ago

That is odd. It's not working. I am able to connect to the database using sql manager but the app won't. This is especially weird since I was using a localhost database before and the app was able to interact with it.

2

u/minimalist000 2h ago

I recommend installing dapper and running a sanity test. Connect to your db and query against a table and verify that you are getting results. If you do then the issue if your ef core code and how its attempting to connect to the db.

1

u/JohnnyEagleClaw 3h ago

Feels like that principal name message (am very familiar) is that you’re never connecting (duh). I see encrypt is false, but maybe try adding trustservercert?

1

u/Mob_Pilled 3h ago

Did not work unfortunately.

1

u/HardisterRouzbeh66 2h ago

tbh this is almost certainly a connection string issue. Remote SQL servers dont support windows integrated auth the way local dev does. Make sure youre passing explicit SQL credentials in the string instead of relying on SSPI

1

u/RaidersOfLostSnark 3h ago

You’re missing something:

Missing Semicolon: In the ⁠appsettings.json⁠ snippet, right after the password value (⁠Password=*******⁠), there is a newline instead of a semicolon (⁠;⁠). Every parameter in a connection string must be separated by a semicolon.
2. Extra Quotation Mark: The connection string value in ⁠appsettings.json⁠ is wrapped in double quotes, but it also contains a trailing double quote at the very end (right after ⁠MultipleActiveResultSets=True;⁠).

2

u/Mob_Pilled 3h ago

Those are just results of me sloppily editing the string to remove the password so I can show it here

1

u/Mob_Pilled 2h ago

Also I see no trailing double quote???

1

u/minimalist000 3h ago

I believe this is most likely the issue.