Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Why localhost may not be a good sqlserver (Cannot generate SSPI context)

Setting up the asp.net forums app on my local XP pro machine took some puzzling. The setup proposes localhost as sqlserver. Running the app resulted in a Cannot generate SSPI context error. There is a MS support article on this HOW TO: Troubleshoot the “Cannot Generate SSPI Context” Error Message. This article is interesting but tries to cover to much. In my scenario it boiled down to sql server being unable to set up a trusted connection to Localhost.

This was the connectionstring (found in the web.config)

<add key=”connectionString” value=”server=localhost;Trusted_Connection=true;database=AspNetForums” />

Changing the server part of the connectionstring to the name of my machine

<add key=”connectionString” value=”server=MyComputer;Trusted_Connection=true;database=AspNetForums” />

resulted in a proper connection and got the app up and running


This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    There us. Using Local you’re requesting the server on the local machine. Using localhost you’re requesting the server on the machine localhost. This is (usually) the local machine but you have to pass a lot of more stacks to get there. In your sql 2000 scenario something is not going right in the latter case. Don’t ask me what; there are a lot of places.

  • PI

    But is there any specific reason to use ‘(local)’ and not localhost in SQL Server connection strings? What makes its behaviour different when we use localhost and why do the connection leaks? The same application when run on SQL 2005 it does not leak the connections. So there is nothing wrong in the code but something related to SQL Server 2000.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Localhost is an IP address where to find the server. All communication goes over TCP at the address

    Local is sql server specific. The communication will not use TCP but something like named pipes.

    As it looks the different versions of your connectionstring lead to a different communication protocol between client and sql-server. Which leads to different behavior.

  • PI

    I am having a C++ app in which I connect to SQL Server 2000 database (Win2K3, SP1). I am using ‘LOCALHOST’ in the connection string. But when I use LOCALHOST, there is a problem of connection leak on SQL Server 2000. Lots of connections remain open through the C++ app. Now when I use ‘(LOCAL)’ in the connection strings, the problem of connection leak goes away. Connections to SQL Server 2000 are closed properly and there is no connection leak on the machine (Wow….). Is it a problem of LOCALHOST? If yes, then what goes wrong while using LOCALHOST?

  • Andrei

    I was trying to set up a system DSN for one of my clients and kept getting the error.
    Adding the server to the host list solved the problem! Thx Ray!

  • Ray

    Possible Solution.

    Hey guys, I found something that fixed my SSPI error. In the docs, it talks all about if this then SSPI, if that then NTLM. The ultimate result is that if Kerberos is being used, and the host name can’t be resolved correctly, then the SPN isn’t generated correctly and the whole thing fails.

    Can’t resolve host name? Check your DNS entry and/or add an entry into the hosts (c:\winnt\system32\drivers\etc\hosts) file on the machine that is trying to connect. This also caused my MS SQL Reporting Services connection problems to go away.

    Hope this helps.


  • Matej


    This is crazy. So so so many people have so many problems with "Cannot generate SSPI context" error and Microsoft still does nothing to solve it.


  • Enjoy Every Sandwich

    In the bag tonight: Less bitch’n and whin’n. Counts:Blogging: 8; Dev: 22; Otherwise: 8; SQL: 5; WILY: 8. Line of the night:

  • Peter van Ooijen

    So do I. Well.. vs.net does so when I drop a connection on a componennt. This localhost thing was an idea of the asp.net forums project.

    Most parts of it look excellent, but this is apparantly a little stupid :/

  • Darrell

    Yeah, I was just pointing out the correct way to use "local" versus "localhost". I always use the server name myself.

  • Peter van Ooijen

    Trying to connect to the SQLserevr "LOCAL" is no guarantee for success. Using the actual name of the server (as seen in the VS.net server explore) does work.

  • Darrell

    In your connection string, the "server" should point to the SQL Server, not your website. Localhost is a term for the local IIS web site. The value of server for a local SQL Server is "local".