Opening SQL Connection from IIS7? - asp.net

I created an ASP.NET website with Visual Studio 2008 sp1 and in the code I use SQL connection to reach a data base in my SQL Server 2008. It all wiorks great but then I wanted to deploy the website in IIS7 (Windows 7) which was hell to discover how to do it-
(I added a virtual directory and converted it to an application and only then it didn't show me the error that I was getting before I copied it to inetpub\wwwroot and converted it to an application). After doing it I tried to run it from the IIS7 but it just keeps showing me an error that it gets in the command sqlconnection.open() which works great when I run the website from the Visual Studio. How do I resolve this?
I am using the following connection string: "Data SourceEDI-PC\SQLEXPRESS;Initial Catalog=SyncMaster;Integrated Security=True"
And my IIS7 is on the same computer as the SQL Server, so what exextly do I have to do to make it work?

Your connection string probably looks like this...
<add name="ConnectionString"
connectionString="Data Source=servername;
Initial Catalog=dbname;
Integrated Security=True"
providerName="System.Data.SqlClient" />
Which means it is using the windows account IIS runs under, NETWORK SERVICE, to connect. When you run from Visual Studio it uses your windows account which has access to the SQL server.
Create a sql user to use as a service account and specify that info in the connection string. This way it will try to connect using the same account that you created in both Visual Studio and IIS.
ie.
<add name="ConnectionString"
connectionString="Data Source=servername;
Initial Catalog=dbname;
Persist Security Info=True;
User ID=sqluser;
Password=sqruserpassword"
providerName="System.Data.SqlClient" />

When running in IIS7 (or any other IIS for that matter) the connection to sql server is made as the user that runs the application pool in iis. In IIS7 this is usually NETWORK SERVICE. So if you are using integrated security, you need to grant this account access to the database. If your SQL server instance is running on another machine, say SQLSERVER01, and your web app is running on your machine, YOURMACHINE, then the NETWORK SERVICE account on your machine will be called YOURMACHINE$ as seen by SQLSERVER01.

Related

ASP.NET Site Connecting to MSSQL as Wrong User with Integrated Security

I'm having problems connecting to an MSSQL database in an ASP.NET Web Forms application. The error I get the is the user corp\earth3 can't connect. earth3 is the name of the computer ... the logged in user is corp\earth (a domain administrator) which is what works when connecting to the database using MSSQL Management Studio. I'm building the web app on Windows 10 and its being deployed for production use on a Windows 2012 server (which has the same connection problem as the win10 machine).
I've tried each of the following in web.config. This works while connecting to other database instances on the db server, but not on this one.
<add key="sqlConnectString" value="Data Source=db01\pd; Initial Catalog=PDB; Trusted_Connection=Yes;"/>
<add key="sqlConnectString" value="Data Source=db01\pd; Initial Catalog=PDB; Integrated Security=SSPI;"/>
What am I doing wrong here? Why can't I connect to the db using corp\earth?
Thanks.
Modify the Identity in the application pool with a user that can connect to the database. Create a custom app pool if you don't want to make a change that would affect other web sites it uses.

no process is on the other end of the pipe sql server 2008

I am developing a web application using VS2012 and SQL server 2008 using windows authentication. When I am executing my application from my local VM it;s connecting to the database and working properly. But when I published the applictaion in a server IIS it is giving this error."no process is on the other end of the pipe". Both are using d same database server. here is my connection string.
<connectionStrings>
<add name="DbConnectionString" connectionString="Data Source=XXXXXX200;Initial Catalog=XXXXX_XXXXX;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Even tried with
Integrated Security=true; and Trusted_Connection=yes;
And also by removing
providerName="System.Data.SqlClient".
But still it's giving error. But the same thing is working fine from development environment. But note from hosted server.
Can you please help me where and what I am missing.
refer this. this will give you instruction troubleshoot the issue
http://technet.microsoft.com/en-us/library/ms175496%28v=sql.105%29.aspx

Kerberos double-hop in ASP.NET 4.0 & SQL2008R2

I have an ASP.NET 4.0 application within which I need to forward the authentication to the database.
For the purposes of this request for assistance, lets call the web server "app1" and the database server "sql1".
The SQL2008R2 database service is running as a named instance "SQL2008R2" under a custom domain account "SqlServer". The server is running Windows Server 2008 R2 Enterprise Edition.
I have created an SPN for this...
setspn -a MSSQLSvc/sql1.mydomain.local:SQL2008R2 SqlServer
The ASP.NET application is running under an application pool using a custom domain account "WebApplicationUser", in Integrated Pipeline mode. It is currently running on my laptop running Windows 7 Enterprise, but will eventually be hosted on Windows Server 2008 R2 Standard Edition.
I have created 2 SPN's for the application (on the Windows 7 machine that I am currently running from)...
setspn -a http/app1 WebApplicationUser
setspn -a http/app1.mydomain.local WebApplicationUser
Within Active Directory users and Computers, I have selected the "WebApplicationUser" account and I have enabled constrained delegation to "MSSQLSvc/sql1.mydomain.local:SQL2008R2" using any protocol (I have also tried using Kerbero only).
The Application is setup in IIS 7.5 and the authentication is set to disable Anonymouse, Basic, Digest and Forms whilst enabling "ASP.NET Impersonation" and "Windows". The Windows authentication has "Extended protection" turned off and "Kernel-mode authentication" enabled. The providers are "Negotiate" and "NTLM" in that order.
The ASP.NET application uses EF, and the connection string is configured to use integrated security...
<connectionStrings>
<add name="MyContext"
connectionString="metadata=res://*/Data.MyModel.csdl|res://*/Data.MyModel.ssdl|res://*/Data.MyModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=sql1.mydomain.local\sql2008r2;Initial Catalog=MyDatabase;Persist Security Info=false;Integrated Security=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
My web config specifies both Windows authentication and impersonation, since I a using async pages, I have also enabled inpersonation policy flowing...
<runtime>
<alwaysFlowImpersonationPolicy enabled="true" />
</runtime>
<system.web>
<authentication mode="Windows" />
<identity impersonate="true" />
</system.web>
If I log on locally (on "web1") and browse to the application (using IE), this all works - but this does not involve the double hop that I am trying to resolve.
If I log on to another machine and then browse to the application using IE, or I browse from the local machine using FireFox, this does not work - note: FireFox does prompt me for the login details. The connection to the database fails with "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'"
Unlike a lot of the articles (and here might be part of the problem), I am not using any custom code to impersonate the user. It is my understanding that the impersonation will be applied across the board to the application by the web.config settings above. All I do is to open the connection and then close it again when I am finished with it.
I have obviously missed a step (or two), but having looked at all of the documentation that I can find (and there has been a lot), I still cannot find what that step is. It does not help that 99% of the documentation that I can find is actually related to IIS6 and Windows 2003 but the principles should remain the same.
Has anybody suceeded in getting such a configuration to work on Windows 7 and/or Windows Server 2008?
Checklist for Double Hop issues {IIS and SQL Server}
http://blogs.technet.com/b/taraj/archive/2009/01/29/checklist-for-double-hop-issues-iis-and-sql-server.aspx
http://www.phishthis.com/2009/10/24/how-to-configure-ad-sql-and-iis-for-two-hop-kerberos-authentication-2/
http://support.microsoft.com/kb/810572
IIS to SQL Server kerberos auth issues
When you configure the SPNs for SQL Server, we have found that we need to include the PORT on which SQL Server listens (1433).
You should download and use Brian Booth's DelegConfig v2 tool to help you setup the correct configuration settings. http://blogs.iis.net/brian-murphy-booth/archive/2009/04/22/delegconfig-v2-beta.aspx
It will basically hold your hand the whole way through the process. We've found the tool to be invaluable.

Creating an ASP .NET User Authentication Connection String using VMWare Fusion with OS Windows 7 on a MAC

I am attempting to create an ASP .NET User Authentication provider on a MAC using VMWare Fusion installed with Windows 7. I have created the SQLExpress datatbase named CustomerOrders, and have a connection to the DB in Server Explorer within Visual Studio 2010.
When I try to use the Web Site Administration tool I receive this error:
The connection name 'ApplicationServices' was not found in the applications configuration or the connection string is empty. (\vmware-host\shared folders\documents\visual studio 2010\Projects\CustomerOrders\CustomerOrders\web.config)
What I can’t figure out is the connection string I need to use in the Web.Config in order to setup the ASP .NET Authentication provider for this project running in a VMWare Fusion environment.
The connection string to my local database is:
Data Source=WIN-NOABML9MSVB\sqlexpress;Initial Catalog=CustomerOrders;Integrated Security=True
First, make sure you have the connection string specified in your web.config:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CustomerOrders;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
(Notice that I use .\SQLEXPRESS instead of the computer name, to make sure I use the local named instance)
Then, if you haven't done so already, make sure you create the CustomerOrders database and run the schema tool to create the database objects you need.

How to copy local Membership and Role database to remote MS SQL Database

I have made a MVC application that uses the built-in ASP.NET login functions. It works perfectly on my local machine. I have bought a webhosting service, because i want to publish my website to the internet.
How do i copy the membership and role database / tables to my new MS SQL Database and make it work with my current project?
In my Web.config i have:
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
notice that
Instance=true
This means that SQL Server Express will open the aspnetdb.mdf file, creating it on the fly first if needed.
When i try to login on my website i get the following error:
Sorry, an error occurred while processing your request.
Thanks
You need to run aspnet_regsql located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. Just point it to your remote sql server and it should take care of the rest.
You will also need to edit your connection string for application services to the one for your remote sql server.

Resources