SQL Windows Auth - asp.net

I have a local test environment set up with IIS. I'm using the connection string below to connect with c# in the codebehind of an ASPX page using my windows auth. I am getting the error that [PCNAME]/ASPNET login failed. Why is the user name ASPNET attempting to login when I've specified my connection string to use my login?
user id=[UID];password=[PASS];server=[LOCALSERVER];database=db_specialOps;Trusted_Connection=yes

Trusted authentication uses the credentials of the user that is executing the process. If it is specified as yes, then the username and password in your connection string are ignored.
In this case, the ASPNET user account is the user that is running the process, so this is the account that is being used to connect to SQL Server.
Checking, another SO question addresses this issue.
When using Trusted_Connection=true and SQL Server authentication, will this effect performance?

Remove the Trusted_Connection=yes part of the connection string. It tells the sql client library to connect to the sql server, using the Windows auth, with the current process' Windows identity. In the ASP.NET case that is [PCNAME]/ASPNET. That's why you see that error message.
If you want to use sql auth, just supply username and password as you do - without the Trusted_Connection=yes part.

Connection string for Windows authentication:
connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes"
OR
connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"
No user/pass
Connection string for SQL authentication with user/pass
connectionString="Server=MyServer; Database=pubs; User Id=MyUser; password= P#ssw0rd"
providerName="System.Data.SqlClient"
Also see this question, might help you:
Connect to SQL Server using windows authentication and specific account

Related

SQLEXPRESS user login error

I'm trying to fire a web application from my local pc and it throws the error "Login failed for user DOMAIN\USER" (please note that I didn't type my actual values for the DOMAIN and USER in the above error).
I connect to SQLEXPRESS
Database is added through MSSMS and a new user is added to the DB with the values from connection string.
Changed authentication to SQL Server and Windows Authentication Mode but still the same error.
Changed connection string with Windows login details but still the same error
Are you intending to login using SQL authentication or Windows authentication? It sounds like you are trying to login using SQL authentication (i.e. named SQL User and password), but your connection string is indicating to use windows authentication (something like SSPI=True)
So you need to verify what login method you want to use: Windows or SQL
Then you need to post your connection string

What is the best authentication mode for asp.net application using SQL Server 2005 Express?

I have inherited an asp.net 2.0 application that uses a SQL Server 2005 Express database and I am grappling with the authentication mode.
I have read that windows authentication is more secure and locally that works just fine. I use the default machine account and a trusted connection in the connection string similar to :
value="trusted_connection=True;server=MYMachineName\SQLExpress;Database=DatabaseName;Pooling=false"
On the server though, I find myself having to grant more privileges to the aspnet user (execute, update) in the database and am wondering if this makes sense after all. The login verification is actually handled by the application.
Does it make more sense to set up a database user and use those credentials in the connection string? Would this be an acceptable connection string?
value="Server=myServerName\SQLExpress; Database=myDataBase; User Id=myUsername; Password=myPassword;"
If you’re not in a need for a very high security you’ll be doing just fine with sql authentication.
Try this connection string instead
<add name="connstring" connectionString="Data
Source=myServerName\SQLExpress; Initial Catalog=myDataBase;
Persist SecurityInfo=True;User ID=myUsername; Password=myPassword"
providerName="System.Data.SqlClient"/>

Can't gain access to local SQL for modification

I've been given a backup of a production SQL database to begin writing an application against.
I've got it running as a local instance and using Windows Authentication and I can READ the information, however I cannot figure out how to get IIS to use my Windows Authentication to log into the SQL server when running a local ASP.Net website.
I don't have the authority on the DB to add users, and I'm getting an error of System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. when I try to access a page that reads the database.
I don't think I will be able to communicate with the DB admin for a while to get him to fix this up for me. Is there anything I can do here?
Your asp code is using the same identity that IIS runs under - the Network Service account. To get it to use your credentials, you can either hard-code them into a connection string or you can use impersonation (see this link: http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx)
The gist of the link:
Put this in your web.config file: <identity impersonate="true" />
-t.
You need to create an account in SQL Server for the Network Service account,ex via SSMS via the "Security" section, right click on "Logins" and select add. You will be adding a Windows Account, you can then lookup and validate the name "NETWORK SERVICE". Then switch to the "User Mapping" section and grant permission to view your database.
reference : http://msdn.microsoft.com/en-us/library/ms998320.aspx#paght000015%5Fsqlserver
2nd Alternative
If "integrated security=sspi" is there in your connection string than you should:
Turn on integrated auth in IIS
Turn on windows auth in asp.net
Turn on imerposation in asp.net
Hopefully it will help.

Error Opening SQL Server Connection on Web Server

Here is my Error:
ERROR [28000] [Microsoft][ODBC SQL
Server Driver][SQL Server]Login failed
for user 'NT AUTHORITY\ANONYMOUS
LOGON'. ERROR [28000] [Microsoft][ODBC
SQL Server Driver][SQL Server]Login
failed for user 'NT
AUTHORITY\ANONYMOUS LOGON'.
Here is my connection string at application start up:
MyConnection = New Odbc.OdbcConnection("DRIVER={SQL Server};SERVER=MySqlServer;Trusted_Connection=True;DATABASE=MyDatabase")
Here is part of Web.Config:
<authentication mode="Windows"/>
<identity impersonate="false"/>
Here is my IIS(7) settings:
Authentication
Anonymous Authentication: Disabled
ASP.NET Impersonation: Disabled
Basic Authentaction: Enabled
Digest Authentication: Disabled
Forms Authentication: Disabled
Windows Authentication: Enabled
Connection Strings
MyDatabaseConnection: Data Source=MySqlServer;Initial Catalog=MyDatabase;Integrated Security=True
LocalSqlServer: data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I believe you may have to use ASP.Net Impersonation in this case.
http://weblogs.asp.net/achang/archive/2004/04/15/113866.aspx
As Zach says, you need to change the credentials of the user accessing the database.
So you either need to turn on impersonation as recommended or you should change the user id and password used by the application pool that is used to run your web site. This MSDN article, while a bit old, still applies and should help you do that.
What user account should be used to connect to the database? Your user account, an account on the SQL server, the IIS app pool account?
By default, if you don't give the connection string a username and password, .NET will use the AppPool's credentials, which as Simen says doesn't have sufficient privileges to the SQL server.
If you want to use the credentials of the user accessing the website (probably a bad idea), you need to use impersonation as Thyamine says, but if you want to use a SQL account that is set up for the database, then you need to put the username and password of that user into the connection string. (http://www.sqlstrings.com/SQL-Server-connection-strings.htm)
if your web application is running without Integrated Security. remove the "Allow Anonymous connections" security option. When authenticated by the web server as ANONYMOUS the request will be executed using an identity does not have sufficient privileges to your SQL Server DB.
Edit: If you are running the web server on a separate machine than the client machine you might want to investigate whether the server actually recognizes the account used on the client machine. If the machines are not part of the same security domain, you might even have to go through the questionable procedure of modifying the server's
Local Policies --> Security Options --> Network access: Sharing and security model for local accounts to the setting : Classic - local users authenticate as themselves and creating a user account with the same name and password on the server.
You might also want to investigate http logs on the server

ASP.NET Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

I'm trying to publish an asp.net website on a remote server. When my code tries to connect to the database, I get the error message described in the question title. Now, I've gone into the Microsoft SQL Server Management Studio for SSE 2008 and can see the list of logins. NT AUTHORITY\NETWORK SERVICE is there, but I'm not sure what settings I need to change to allow this user to access the database. I've tried using impersonation in my web.config file using the windows login credentials for the server, but that just brings up the same error message, with my windows username instead of NETWORK SERVICE.
My connection string is as follows:
connectionString="Data Source=MECHTRONICRND\SQLEXPRESS;Initial Catalog='C:\Inetpub\aspnettest\App_Data\FLEETMANAGERDB.MDF';Integrated Security=True"
Any ideas?
Thanks
--Amr
Thanks for your replies. After looking at this tutorial, I found out how to allow users access to a database. Once I had allowed NETWORK SERVICE read and write access to the database, my website worked fine with the original connection string.
--Amr
Your connection string should be:
"Data Source=MECHTRONICRND\SQLEXPRESS;Initial Catalog=FLEETMANAGERDB;Integrated Security=True"
Also, this is trying to connect to the database as the account that's used by the web server. You could run the web service (configure IIS accordingly) as a domain user, and then create a login and database user for that account. Otherwise, you will have to create a database user (in FleetManagerDB) for the Network Service account, which isn't recommended. Or yourself if you are impersonating yourself.

Resources