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

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.

Related

IIS server not loading the database

I have created a website using asp.net every thing is running fine when i run this using visual studio development server but when i tried to run it by IIS server then Home page of website loads but it couldn't make connection with database server
<add connectionString="Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=CloudStorage;Integrated Security=True" name="connectionStr"/>
</connectionStrings>
this works fine on visual studio develoment server but not with IIS.... Please help
Since you are using Integrated Security=True you use the application pool identity to connect to the DB. When you run Cassini, you use your own credentials. You should set up the application pool identity with a user that has permissions to login and query SQL Server.
From MSDN:
When you run a page using the ASP.NET Development Server, the page runs in the context of your current user account. For example, if you are running as an administrator-level user, a page running in the ASP.NET Development Server will have administrator-level privileges. In contrast, in IIS, ASP.NET by default runs in the context of the special user (ASPNET or NETWORK SERVICES) that typically has limited privileges. The ASPNET or NETWORK SERVICES accounts are local to the server computer (not domain accounts), which restricts access to resources on other computers.
Database access
When working with the ASP.NET Development Server, you can typically rely on Windows Integrated authentication to access SQL Server. However, when the same page runs in IIS under the ASPNET or NETWORK SERVICES account, the page is running in the context of a local user, and you often have to configure the page to use a connection string that includes information about a user and password. For details, see Accessing SQL Server from a Web Application and ASP.NET Security Architecture.

windows azure AspNetSqlMembershipProvider

I am working on an ASP.NET application that uses the AspNetSqlMembershipProvider for the authentication mechanism.
I am transferring the application to a cloud application in order to deploy it on windows azure.
Do I have to change the authentication mechanism that I use or will the AspNetSqlMembershipProvider will work normally?
Don't change the authentication mechanism, just use the Universal Providers and it will work normally.
You have no need to change the Authentication Mechanism for the reason of deployment. Just make the local machine database scripts to run on the sql azure database. Everything should be work same as you are doing authentication on local development machine.
Check these links to know regarding this deployment of sql authentication or database script etc:
Trying to deploy Asp.Net Memebership database to SQL Azure
Updated ASP.net scripts for use with Microsoft SQL Azure
After installing the scripts, just change the connection string in the webconfig that provide the authentication through database.
<connectionStrings>
<add name="LocalSqlServer"
connectionString="you azure connection string"
providerName="Provider..." />
</connectionStrings>

Connection string setting while deployment

I have ASP.NET project and attached mdf database.I want to deploy it to remote server.I have just ftp account.The project works well on my local but ı had problem while deploying.I changed my connection string like. Please help me what is the problem ?
<add name="libraryConnectionString"
connectionString=" Server =.\SQLExpress;
AttachDbFilename= myusername#ftp.myserver.com/App_Data/LIBRARY.mdf;
Integrated Security=SSPI;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
You have to create a Ms-Sql server database (remote) along with user via control panel or you may ask to your hosting provider to create a database and user. Once database is created then you may execute your script (You may use Visual Studio Database Publishing wizard) and construct a new connection string for your web-app.
Have a look at MSDN article - Deploying a Database by Using the Database Publishing Wizard
The Database Publishing Wizard in Visual Studio enables you to deploy
a SQL Server database (both schema and data) to a hosting environment.
You can run the wizard by right-clicking a database in Server Explorer
and then clicking Publish to provider.

SQL 2008 + IIS Application Pool: Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path

I´m having trouble connecting to SQL Server 2008 R2 Express with Northwind database using ApplicationPoolIdentity in IIS 7.5, resulting in the following error message:
Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. The connection will be closed.
notes:
I´m using Windows 7/VS 2010/IIS 7.5/SQL Server 2008 R2 Express (all in the same machine).
my site is using "ASP.NET v4.0" application pool;
ASP.NET v4.0 application pool is using ApplicationPoolIdentity as Identity;
the SQL Server 2008 R2 express has the Login "IIS APPPOOL\ASP.NETv4.0" with the server role sysadmin granted;
the Northwind database has the user "IIS APPPOOL\ASP.NET v4.0" configured with the following permitions: db_accessadmin, db_backupoperator, db_datareader, db_datawriter, db_ddladmin, db_securityadmin;
the w3wp.exe process is running with the user "IIS APPPOOL\ASP.NETv4.0"
I can see that the user profile folder is created (C:\Users\ASP.NET v4.0);
the same problem occurs using the application pool "DefaultAppPool".
If I change the ApplicationPoolIdentity user to another user that has rights to access the database it works.
Additional informations:
connection string:
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=NOTEBOOK\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Could anyone help me to solve this issue?
Thanks in advice.
It looks like you're trying to use SQL Server Express's "user instancing" feature, i.e. your connection string looks like:
AttachDBFilename=|DataDirectory|xxxxxx.mdf;User Instance=true
This won't work for ApplicationPoolIdentity accounts in IIS7.5. For more information see this MS Knowledgebase article:
Problems with SQL Server Express user instancing and ASP.net Web Application Projects
Web applications built with Visual Studio 2005, Visual Studio 2008, or
Visual Studio 2010 and that rely on user instancing with either SQL
Server Express 2005 or SQL Server Express 2008 do not work with the
new application pool identity. These products were developed and
tested against application pools running with the older NETWORK
SERVICE account.
Also Microsoft discourage using this feature now:
This feature will be removed in a future version of Microsoft SQL
Server. Avoid using this feature in new development work, and plan to
modify applications that currently use this feature.
If you've not already done so, I would attach the database to SQL Express and use a regular SQL connection string instead, just like you'd do in a production environment. You may need to enable TCP/IP for your SQL Express instance using the SQL Server Configuration Manager if it's not already enabled:
And ensure that port 1433 is configured as well on the next tab of that dialogue.

Opening SQL Connection from IIS7?

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.

Resources