Defining connection string using windows authentication across domains to access SQL server - sql

I have problems defining the connection string in a windows service application. The SQL server and windows service are on different domains, and thats the problem. However, i know this is possible, because when using runas.exe in the terminal with this parameters there is no problem.
%windir%\system32\runas.exe /noprofile /netonly /user:DOMAIN1\%USERNAME% "ssms -nosplash -S SQLServerName -E"
So the problem should be in my connection string. How can i modify my connection string to access the server the same way? My connection string now looks lite this:
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/DataAccess.MyDatabase.csdl|res://*/DataAccess.MyDatabase.ssdl|res://*/DataAccess.MyDatabase.msl;
provider=System.Data.SqlClient;provider connection string="data source=SQLServerName;
initial catalog=MyDatabase;Trusted_Connection=yes;User Id=DOMAIN1\USERNAME;Password=*****;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
UPDATE
Seams like it isnt possible to replicate the runas.exe command exactly. I tried to use impersonation like podiluska explained. But, this only works if the user can run on the current domain. In my case i cant, I want to send the windows credentials as parameters when connecting to the database, because that user cannot log on in the current domain. Can anybody confirm that this is impossible?

You need to use impersonation - either in the IIS app pool, or by adding a line to the web.config
<identity impersonate="true" userName="DOMAIN1\UserName" password="****" />
(and remove the user id / password entries from the connection string

Related

Database connectionstring windows authentication but passing user and password

I need some help.
I'm trying to connect to my database.
This is my connectionstring :
<add name="DefaultConnection" connectionString="Data Source=KBV-SQL12-TEST\KBSQLSPTEST; Initial Catalog=KBSPT_aspnetmembership; User Id=KBOVDM01\KBSPT_EXTRANET; Password=SPExtr#n3t" providerName="System.Data.SqlClient" />
The user is an Active Directory user.
When I go to my SQLServer and log on as KBSPT_EXTRANET, I can op the SQL Management Studio and connect to the database.
But in my application when I use the connectionstring mentionned above, I keep getting the message "Underlying provider failed on open".
What am I doing wrong?
You are trying to use a Windows account when the connection string is expecting a SQL Server user account.

Strange MVC Error Using GearHost

When I run my program on my local machine it works just fine. However, when I try to interact with my database via GearHost, I get this nugget of an error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified.
Everywhere I have gone, the forums say it is a connection string error.
If that is the case here is my connection string:
<add name="GameStoreEntities" connectionString="metadata=res://*/Models.GamesModel.csdl|res://*/Models.GamesModel.ssdl|res://*/Models.GamesModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\GameStore.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
If anyone has any idea about what is wrong with the connection string, I would appreciate your feedback.
GearHost does not seem to have support for LocalDB.
So you would have to create a database on their admin panel and use the credentials of the default user that is created to write your connection string. You could also create a new user for the database and use those credentials instead.
Either way your new connection string should be something like this
<add name="GameStoreEntities"
providerName="System.Data.SqlClient"
connectionString="Data Source=DATABASEHOST;Initial Catalog=DATABASENAME;Integrated Security=False;User Id=USERNAME;Password=PASSWORD;MultipleActiveResultSets=True" />
where you just replace the DATABASEHOST with the hostname for the database. You would see this at the bottom of the page after creating the database.
You get the PASSWORD by clicking the uncover(eye) button after creating the database
If you have data in the localDB that you want to sync to the database you have created on GearHost you can use the Data Comparison and Schema Comparison tools in Visual Studio to update the target database.

What does App=EntityFramework do in Sql connection string?

I have 2 connection strings - 1 local and 1 for my main production server. Entity Framework added App=EntityFramework to my local string when I installed it (4.1) - I'm now on 4.3. What does this do - I can't find any reference to it?
Here's my local connection string:
<add name="LocalConnection"
providerName="System.Data.EntityClient"
connectionString="metadata=
res://*/;
provider=System.Data.SqlClient;
provider connection string='
Data Source=.\SQLEXPRESS;
AttachDBFilename=C:\mypath\MyDb.mdf;
Integrated Security=True;
User Instance=True;
MultipleActiveResultSets=True;
App=EntityFramework'" />
Just curious!
App and Application Name are simply a way for somebody debugging SQL Server to know which client is connecting to it. If you had a SQL Server that has several apps that used it, it might be hard to know which one was sending which statements. If each app used a different Application Name it would be very clear.
Check this out for more info.
It's just the synonym of the Application Name.
You can see the Connection String properties outlined here:
http://msdn.microsoft.com/en-gb/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

.mdf file is not created on ASP.Net MVC 4 app after adding <connectionStrings> tag on Web.config

I was trying the ASP.Net MVC 4 tutorial. When I have added the following code and build the application I was expecting Movies.mdf file will be created under App_Data folder on the Solution Explorer. However, nothing has been created in that directory. Any idea why?
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true"
providerName="System.Data.SqlClient"/>
<add name="MovieDBContext"
connectionString="Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Using:
.NET Framework 4
VS 2010
Having a full instance of MS SQL 2012 on my machine, not SqlExpress, I had this problem. I found that by letting the application continue (instead of halting it in visual studio when VS catches the exception) I got a helpful message in the browser:
Directory lookup for the file "c:\users\chris\documents\visual studio
2012\Projects\MvcApplication1\MvcApplication1\App_Data\aspnet-MvcApplication1-20140225162244.mdf"
failed with the operating system error 5(Access is denied.). CREATE
DATABASE failed. Some file names listed could not be created. Check
related errors.
which tells that the problem is that the sql service doesn't have write permissions to my application directory.
Giving it permissions was slightly tricky because the username needed didn't come up (in Windows 8 Pro) in the Explorer GUI for Security. The username for a Default Instance of Sql Server is NT SERVICE\MSSQLSERVER (for an instance it's NT SERVICE\MSSQL$InstanceName) and I had to type it in by hand, and then give it write permissions. I did this just on the App_data directory.
Then it worked.
The problem is solved. The problem was the location of the project. I was working with the files from a shared drive which apparently did not create the database in the App_Data folder. Connectionstring code is absolutely fine as it is shown in the tutorial and no changes are required.
The database will not be created before you actually add the first record to one of your tables.
This error occurs when Visual studio and MSSQL are running under different authorities. For this reason, your application doesn't get to have a write permission for creating the .mdf database file.
SOLUTION
Open SQL Server Configuration Manager
Select SQL Server Services
See the Log On As tab
the value would be something like NT AUTHORITY\NETWORKSERVICE ( it depends )
Once you get the user name,
then go to the target folder. ( If default, App_Data in your project directory )
right click -> property windows -> security tab
click edit -> check the user or group name
You shall not see there's no Network Service on the user or group name list
All you have to do is to add the user name and give the write permission for that user or group.
It may be the case of AttachDBFilename... I have this for my project and it works fine.
... connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Movies.mdf" providerName="System.Data.SqlClient"
Also verify your model is correct. If your model does not initiate this connection then it will not create the database.
I have resolved the issue by modifying the connectionstring like below
<add name="MovieDBContext"
connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
I did install Microsoft SQl Server Compact 4.0 from nuget.. but I am not sure whether it is really require to install it or not.
In this MVC tutorial, just add "Initial Catalog=Movies;" (without quote) to the connection string and it'll work well.
<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

SQL Error Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection when I'm trying to open the WebPart

I've created a custom Web Part for SharePoint that interacts with SQL.
Everything worked fine on my DEV server.
After I moved the WebPart to the client's server I started having problems.
I get Error Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection when I'm trying to open the WebPart.
I've searched for solution for a few hours by now and everything I have found doesn't seem to work in my case.
This is how my connection string looks like:
<add name="MyDataEntities" connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
provider=System.Data.SqlClient;provider connection string="Data Source=ServerName;Initial Catalog=DBName;
Trusted_Connection=yes;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The SharePoint Web App with the web part and SQL DB are on two different machines.
Here's what I've tried:
1). Made sure SQL uses Mixed mode authentication
2). Made sure the account I'm using has rights to access SQL
3). Tried replacing Integrated Security=True; in the connection string with the User ID = UserID; Password=Password; where UserID and Password were the account IIS is running under.
I ran profiler while clicking on the link and it looks like the app is not using the account’s credentials and is trying to log in anonymously.
Any help is appreciated, I'm desperate because this must be up and running by tomorrow.
Thanks in advance!
Try SPSecurity.RunWithElevatedPrivileges: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx
This method will run code as the ASP.Net application pool identity. Wrap your database calls with it.