"Cannot Generate SSPI Context"It's the DEVIL! Your app can't authenticate to SQL Server because... Authentication, Active Directory, Kerberos and SPNs... and Delegation ...and DNS ...and too many answers on Google! The answer is in there somewhere, but where?? |
|||||||||||||||||||||||||||
SSPILet's start at the (relative) beginning... SSPI - Security Support Provider Interface It's the common front to several Security Support Providers (SSPs)
You're probably reading this because you're trying to open a connection from an application to a SQL server, you're application is using Integrated Security and it's not working. In this case, Windows will try to connect with the Kerberos Authentication Provider first. You can bypass Kerberos, but why? Let's get Kerberos working. |
|||||||||||||||||||||||||||
How Kerberos Works
|
|||||||||||||||||||||||||||
Walk the Walk | |||||||||||||||||||||||||||
Check DNS | |||||||||||||||||||||||||||
Your client will try and connect to the SQL server with a connection string. That Connection String contains a 'Server' reference.
In this case we're looking for 'MySQLServer' On the computer you're running your client application, ping that address and make a note of the IP Address you get back. Don't worry if you don't get a reply, we're not checking if ICMP is open to your SQL server. If the name isn't found and you don't get an IP Address back, you have a DNS issue. Your SQL Client will create an invalid SPN from the original server name and fail to authenticate. |
|||||||||||||||||||||||||||
Next, ping the IP Address with a '-a' switch to see what FQDN DNS returns. In this case we're looking for 'MySQLServer.MyNetwork.com' This is the server name used to create an SSPI connection. If the FQDN isn't returned, you have a DNS issue. Your SQL Client will create an invalid SPN from the original server name and fail to authenticate. |
|||||||||||||||||||||||||||
Lets just make sure we can connect to SQL on this address. If you can't connect, there's a good chance either:
|
|||||||||||||||||||||||||||
Still not Working?Check ADOnce your SQL Client has resolved a valid FQDN and built an SPN (Service Principal Name) string, it is sent to AD to find a match. Matching SPNs are stored in the Active Directory object of the login used in the SQL Server service.
When SQL Server starts, it tries to write it's own SPNs into AD. There's a good chance your SQL Server Service account doesn't have Domain Admin priviledges (I hope it doesn't) so this will fail and your SPNs will be missing by default. You can manually check if the correct SPNs exist with the following PowerShell command:
You should get the following back:
Where the line starts 'MSSQLSvc' and 'MySQLServer.MyNetwork.com' exactly matches the server name you got back from 'ping -a'. You can safely ignore any records that don't start 'MSSQLSvc', your SQL Client will too. If you have no 'MSSQLSvc' SPNs returned from your Get-ADComputer command, or they are incorrect, you can alter the recorded SPNs with the following PowerShell commands:
|
|||||||||||||||||||||||||||
Valid SPN Formats
|
|||||||||||||||||||||||||||
Add SPNs to AD Objects AutomaticallyIf you're scared of adding and removing SPNs manually in AD, you can run the Microsoft 'Kerberos Configuration Manager' Tool to automate the process. You have to run it on your SQL server, as Administrator. If it fails to launch, check that you don't have any unresolvable users in your local 'Administrators' group.
|
|||||||||||||||||||||||||||
Static PortsFor a TCP connection, your SQL Client will include either of the following in the SPN it sends to AD:
You will need to fix the TCP ports your SQL Named Instances use, so the SPNs registered in AD remain valid. |
|||||||||||||||||||||||||||
TimeThe Kerberos protocol uses timestamps in its communications to secure itself from replay attacks. By default, a Windows Domain will have a policy set to allow a maximum deviation of 5 minutes. If your client, SQL or AD are more than 5 minutes out, Kerberos authentication will fail. If you're seeing intermittent Kerberos Authentication failure, check the time on each of your Active Directory Domain Controllers. Check if anyone has changed or overridden your Default Domain Policy here:
|
|||||||||||||||||||||||||||
Check Your ConnectionsThe final piece of the puzzle is to check you can now connect with Kerberos Authentication. Make your connection and login with your original Connection String. Check in SQL to see if your connection successfully negotiated:
|
|||||||||||||||||||||||||||
All doneI hope that helps! |