Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot login to SQL Server using NHibernate, but with Management Studio or sqlcmd it works

Tags:

c#

sql-server

Im getting login failed error when trying to connect to my database using NHibernate:

var config = Fluently.Configure()
              .Database(MsSqlConfiguration
              .MsSql2005
              .ConnectionString("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=xxx"))
              .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));

With Error 18456 State 5 in the log viewer in SQL Management Studio.

But using sqlcmd or Management Studio it works fine (using Windows Auth)!

What could be the problem?

EDIT The NHibernate exception:

Test method ProjektLogg.Tests.NHibernateTests.ShouldBeAbleToGenerateFactory threw exception: 
FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 ---> NHibernate.HibernateException: Login failed for user XXX-INTRANET\username. ---> System.Data.SqlClient.SqlException: Login failed for user XXX-INTRANET\username.
like image 923
ErikTJ Avatar asked Oct 22 '22 19:10

ErikTJ


1 Answers

That error message means "Invalid UserID". If you're running IIS, make sure your application pool uses the same identity as the user who you are connecting to the database with in management studio.

If you're not running under IIS, make sure your current user account (Windows) can access the database, as you're using Integrated Security.

Link on the error: http://blogs.msdn.com/b/sql_protocols/archive/2006/02/21/536201.aspx

EDIT: Hang on, you're missing Initial Catalog. Try this:

"Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=YourDatabase"
like image 109
mattytommo Avatar answered Oct 24 '22 12:10

mattytommo