Is password necessary in this circumstances? - c#

When I use below part in the web.config, it gives error
(System.Data.SqlClient.SqlException: Login failed for user 'John'.)
<appSettings>
<add key="ConnectionString" value="server=.\SQLEXPRESS;database=Survey;Integrated Security=False;User Instance=True;User=John;"/>
</appSettings>
But when I open the SQL management studio, I do not enter any password.
Authentication seems Windows Authentication and there are user name and password which are disabled, I cannot enter anything and although the password is empty when I click the connect button, I can connect database but from the asp.net project I cannot connect because I get the Login failed for user error.
How can I solve this problem?
When I wrote Integrated Security=False, it is needed to enter password, but it seems empty.
When I wrote Integrated Security=True, I get below error:
"Cannot open database "Survey" requested by the login. The login failed.
Login failed for user.."
Note: I use SQL Server 2008, asp.net 4.0, and c# programming language.

If you use Integrated Security, then the current Windows user account is used. In ASP.NET, this is whatever you have configured for the application pool.
If it's set to False, then it will use the User Id and Password credentials you supply - which must be SQL Server credentials.

If management studio is set to windows authentication you log in using your current windows credentials. you can edit the connection string and add your windows password. or you can set the sql server to windows/sql authentication mixed mode and add a new sql login with a new username and password and use that one instead

Related

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication in c# application

I have c# application that run on a client machine and have access to remote PC database, but when i run the program it displays
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication" error.
I try all the things. I try this:
Data Source=Elias;Initial Catalog=FIDs; user id=sa; Password=serverpassword;Integrated Security=true;`
And this:
Data Source=192.168.42.2, 1433;Initial Catalog=FIDs; user id=sa; Password=gezahegnelias#gmail.com;Integrated Security=true;
You need to remove Integrated Security=true; from the connection string. This is telling your application to use your Windows credentials instead of the username and password you are supplying.

Correct Connection string for accessing remote database server

I have developed a c# program I want to use to access a remote server from a client computer on the same domain.Every time I try to connect to the database I am getting an error login failed for user domain\myclientusername
Using the connection string below:
<add name="dbname" connectionString="Server=remoteservername;Database=dbname;Integrated Security=True;"/>
I have already checked and the server is set to allow remote connections. I am able to log on to the server using admin credentials do I need to set the connection string to use admin password and user name to connect to the database? or do I need to add my client profile credentials to the database permissions. Very new to deploying applications.
You will need to set up access to the database for the connecting user/client PC. You sort of "map" a user on the domain to a SQL user or group
Go to SQL management studio> connect to your database> expand databases > expand 'security' and then 'logins'
right click logins and select new login. search for the user of the connecting user/client PC. Then under the "user mapping" tab you can give access to the specific database. it may be best to give "db_owner" at first just to ensure you have the connection. after that, you should limit the access to only what is needed.
you can always check the SQL events log from the SQL machine itself and see what the specific authentication issues may be. go to "Events" in the Administrative tools (which is in control panel) and you can see SQL specific events.
Either you can remove the Integrated Security=True in your connection string and insert the username and password of a Login you create in you SQL Server database to your connection string.
Or, you can create a login for the user under which your c# program is running (yourself - for testing, domain service account under production) to the SQL Server and give it appropriate read/write access.
More information on connection strings: http://www.connectionstrings.com
More information on how to create login in SQL Server : http://msdn.microsoft.com/en-us/library/aa337562.aspx
When you set Integrated Security=True , the current Windows account credentials are used for authentication.
Since you are trying from a different PC than the one that is running the SQL instance it is much likely the acount you are trying to connect with differs from the acount registered to the instance log in.
what you can do is:
Use Integrated Security=sspi and provide the login credentials, e.g:
connectionString="Server=remoteservername;Database=dbname;User id= myUser; Password=myPass;Integrated Security=sspi";
you can also set Integrated Security=false and also provide the credentials, (but the connection won't be using Windows Athentication)

IIS APPPOOL\MyAppPool unable to log in to my SQL Server database

I've got a local WCF web service project that I'm trying to get to access my database. This is my setup:
SQLServer 2008 R2 Express
IIS 7.5
Using IIS APPPOOL\MyAppPool application pool
The AppPool is set to target .Net 4.0 and its identity is set to ApplicationPoolIdentity.
The AppPool user is added in the database and has been assigned dataReader and dataWriter rights. I've tried adding the user to the database both as a "Login" under Security\Logins and as a user under MyDatabase\Security\Users.
Since I'll eventually switch to sql server authentication, I also tried using a real windows user that I assigned reader/writer rights in the database.
I then tried converting the ApplicationPool's identity to NetworkService and added the NT AUTHORITY\NETWORK SERVICE user to the DB aswell but with equal lack of success.
This is the connection string that I'm currently using (With integrated security):
Server=.\SQLEXPRESS;Database=MyDatabase;Integrated Security=true"
As soon as I try to interact with the database in my web service code I get this error:
System.Data.SqlClient.SqlException: Cannot open database "MyDatabase" requested by the login. The login failed. Login failed
for user 'IIS APPPOOL\MyAppPool'.
When using other users than the ApplicationPool then their user names are displayed instead of the IIS APPPOOL one.
Does anyone have any idea what I could've missed?
UPDATE:
With some help from Oded and Tomek I'm now pretty sure that it has to do with the SQL Server. When using SQL Server Authentication I get this error (In the windows event log)
Reason: An attempt to login using SQL authentication failed. Server is configured for Windows authentication only.>
Using Integrated Security (IIS APPPOOL\MyAppPool user) I get this error in the event log
Reason: Failed to open the explicitly specified database.
The server is configured to use "SQL Server and Windows Authentication mode" though which puzzles me. It seems like the second message simply means that the credentials were wrong, which also seems weird since the AppPool user does not have a password. Using the script posted by guptam in post 6 here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=121202 does NOT show my IISAPPPOOL user nor the one created for SQLServer Authentication. The users do exist under both Login and Users and they have the correct rights assigned.
The connection string should be:
"Server=.\SQLEXPRESS;Database=MyDatabase;Integrated Security=SSPI;"
The value for Integrated Security is SSPI, not True.
There is an alternative syntax:
"Server=.\SQLEXPRESS;Database=MyDatabase;Trusted_Connection=True;"
The key here is Trusted_Connection, not Integrated Security.
I suggest taking a look at connectionstrings.com - a good resource for correct connection strings.
Go to your IIS Manager -> ApplicationPool. Right Click your website ApplicationPool and chose Advance Settings. And change Identity to LocalSystem.
Reference
Can you try these steps (some steps of your description are a bit unclear). Let's try to use "sql server authentication"
Create Login Security->Logins, mark "sql server authentication", provide password, untick "user mast change password at next login"
Select "default database" to the one you use
Go to "User Mapping" page and select your database
Use below connection string with user you just configured
connectionString="Data Source=YourDbServerName;Initial Catalog=YourDbName;User ID=YourLogin;Password=YourPass"
I eventually decided to reinstall SSMS and to recreate my database from scratch. It's now up and running as it should. I must've had something fundamentally wrong in some basic setting. Sry for all the confusion and thx for all the help!

Cannot open database "dbname" requested by the login. The login failed. Login failed for user "machinname\username"

I am trying to run a console application written in C# using visual studio 2010
in which i am accessing a temp database of Microsoft Sql Server Managment Studio.
But the application gives exception as follows:
"Cannot open database "dbname" requested by the login. The login failed. Login failed for user `machinname\username""
And my connection string is as follows:
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Initial Catalog=temp;Integrated Security=True";
Why might this fail with such an error?
The user that the application is running under (probably you since this is a console application), does not have login permissions on the database.
Integrated Security=True indicates that Windows Authentication is being used - either use a user that has appropriate permissions on the database, or grant the appropriate permissions to the user.
If you change the connection string to have a sql server username and password that can be associated with the database then it will work if the application is not running under a windows identity that is not associated with sql server windows authentication
Data Source=.\SQLEXPRESS;Initial Catalog=temp;Persist Security Info=True;User ID=<username>;Password=<pw>"
the application is running under a user that is not recognised in sql server

How do I make my application Integrated Loginable?

I am always doing the following Login module:
Create a table with Username and Password
Login by checking the user table.
So how do I use integrated Login just like the application like Sql Server? I don't mean by passing a connection string, I mean it will check the credential of my application without putting any username and password.
Thanks
EDIT:
I don't mean the SQL Server integrated Login, I mean my application's integrated login, is it possible?
For example, my application will read the AD's name and compare to the current username, then I don't need the user to type in a password for my application.
I am not sure how do I do that and where to start, since if I search Integrated Login, 99.9% of the results from Google will give me the connection string, which I am not looking for that.
[This answer assumes that you're on an Active Directory domain]
Are you using active directory windows authentication? If this is the case you can use the integrated security option in the connection string.
Here's an example connection string using integrated security:
"Data Source=MyServer;Initial Catalog=db name;Integrated Security=True"
This would reduce the complexity allowing you to forget about having to ask the user for a username and password because it'll pick up the windows credentials that they've logged into the workstation with.
Edit:
If the application is a website then you'll need to set the SQL server as "trusted for delegation" in active directory (see http://msdn.microsoft.com/en-us/library/aa905162(SQL.80).aspx). This will allow the webserver to delegate the user's windows login credentials to the SQL server. If you don't do this then the SQL connection will be anonymous.
Assuming that your application will be running with Active Directory on the local network I think there are a few options, depending on how do you want to implement authorization.
If you want/need to do authorization with some custom rules/logic then the first thing you need is to determine who ran the application:
Environment.UserName in conjunction with Environment.UserDomainName will give you enough information;
Alternatively (and more secure) you have use the following code snippet:
AppDomain.CurrentDomain.SetPrincipalPolicy(
System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
var identity = Thread.CurrentPrincipal.Identity;
From the identity you can extract Security Identifier which is designed to be globally unique. (But not that pretty as domain\username pair.)
After getting current user's name you can apply whatever authorization rules you want.

Categories