21

When trying to run my .NET Core 2.0 app from IIS Express, I get the following error:

System.Data.SqlClient.SqlException: 'Failed to generate SSPI context.'

This occurs when trying to access the DB on the SQL 2012 DB server using the following connection string (username and password redacted):

"Server=10.10.127.170;Database=NGProd;user 
id=*;password=*;Trusted_Connection=True;MultipleActiveResultSets=true;"

I'm using a SQL Server Login and password created specifically for this app. I can log in to SSMS with it and have all access to the DB. I can test the connection on other computers and it works fine. I also checked the SQL Error Logs and no errors are being reported when I try to log in through the app or through SSMS.

I've gone through several blogs, sites, and forums looking at "Cannot generate SSPI context" errors, but none of those resolutions have had any effect.

Anyone have any ideas? I'm stumped!

2
  • There is a huge range of problems that might be resulting with this exception. One of the reason might be- You are trying to connect to remote server using domain credentials from a computer that is not under active-directory. Are you sure that you are using sql credentials in a connection string?
    – Maris
    Feb 28, 2018 at 14:06
  • Yes, it's a SQL Server login and not a domain login. I set it up on the SQL server myself and confirmed that it works. I've even confirmed it works in SSMS on computers that are not on the domain, it just doesn't work in my app. Feb 28, 2018 at 16:28

6 Answers 6

52

Set the parameter Trusted_Connection=False in your connection string. Or, if Integrated Security is present, set that to False instead.

2
  • 3
    Integrated Security=False could helps too Jun 26, 2019 at 5:03
  • Or if you want to connect with Windows Authentication you can enable this in the Credential Manager. Use the database url (or ip?) and then add the port with a colon like :1433, and add your username and password.
    – Rubenisme
    May 21, 2021 at 8:20
8

I've had same problem recently. I removed the Trusted_Connection part from my connection string:

"Server=10.10.127.170;Database=NGProd;user id=*;pwd=*;"

and it worked for me.

0
5

Trusted_Connection should be set to false:

Trusted_Connection=False
3

As it has been said, it can be caused by a range of issues. One of them is the AppPool Account in IIS. Confirm that it has permissions or you can update it from the default built-in account to the account you want to access the server with. Under the App Pool tied to your app, got to Advanced Settings > Process Model > Identity. Then choose Custom Account and enter your server account. See the screenshot below.enter image description here

2
  • 2
    Great, except I'm running this from IIS Express at the moment, so this doesn't help. Feb 28, 2018 at 20:41
  • 1
    I was running the app pool as myself, and my password expired. I didn't realize it until I read this. "Logging" back into the app pool with my new password fixed it. Thank you!
    – codeMonkey
    Dec 3, 2019 at 16:35
2

You're mixing credential type. You should either use Integrated Security (aka Windows Authentication) or username and password, not both. Either use Trusted_Connection=true and give your personal user authorization on the remote server (which I'm guessing isn't the case right now), or set Trusted_Connection=false and use the username and password of a user that is authorized.

Also, as a side note, this was already pointed out in another answer, but, while IIS Express runs under your user account, this is not necessarily the case once you deploy to a remote IIS server. If you're using Integrated Security between an IIS Server and a remote SQL Server instance, you'll need to grant login permissions to the IIS Server itself to make this work (or have it run under something other than an App Pool user, which is just a virtual account and doesn't have an actual user in Active Directory).

If you're using a username and password, the previous paragraph is a non-issue, but obviously you'll need to store the connection string securely.

1

After much banging my head and hours of trying every blog post and suggestion I could find, I had a wild idea. I simply linked the remote SQL server to my local SQL instance using the SQL Server credentials I created. BAM! Works like a charm now, even if I do have to write a bit extra into my queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.