views:

34

answers:

2

I have an ASP.NET application that authenticates users using Ldap against active directory. This is, the users enter their same windows credentials on the webform to login to this application. The application is basically a SQL Server database frontend, and in my limited experience, I use the standard connectionStrings label in the web.config to enter the information to login to the database (using a SQL Server login as of right now). I want to change that so instead, each time the user connects then he or she perform the database operations with his or her windows user. Of course the database has setup those permission already to the users, but I have no idea what connection string am I suppose to use, since now there is not going to be a static user/password combo, and I don't want to create SQL Server logins to everybody that is going to use the application.

Thanks!

+2  A: 

Use the ConnectionStringBuilder class and provide it your initial connection string from the web.config. From there, change the username and password properties to match those of your current user.

public String GetUsersConnectionString(CurrentUser user) {
  string connectString = ConfigurationManager.ConnectionStrings["LocalSqlServer"]
                        .ConnectionString;

  SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
  builder.UserId = user.UserId;
  builder.Password = user.Password;

  return builder.ToString();
}
villecoder
This would require the web page to know the user's active directory password. Typically not true for IIS running Windows authentication
Andomar
@Andomar At some point during authentication, you're going to have to get the user's password and authenticate it against the LDAP source. At that time, if the LDAP source successfully authenticates the user, you could create the connection string and store it in the user's Session state. From that point on, all web pages know what the connection string is.
villecoder
@villecoder: That would only work in basic authentication, when you send plaintext passwords over the wire. In most cases, the server asks the client for an MD5 hash of the password added to some other text. The web servers won't know the password.
Andomar
@Andomar The OP states that they are using a windows form to perform LDAP authentication against Active Directory. Keyword here is windows form, meaning the application is aware of the user's password as a POST variable. The server isn't going to hash that value and then pass it to the code behind. I'm not sure where the MD5 is coming in if the authorization mode has been set to Forms.
villecoder
@villecoder: On rereading looks like you might be right, though that would be highly unusual, sending passwords over the wire! +1 anyway
Andomar
+1  A: 

You would have to configure the ASP.NET machine for delegation. See this knowledge base article for SQL 2000, or a newer one for SQL 2008.

We have tried it a couple of times at work, but did not manage to get it running smoothly.

Andomar
+1 Thanks for your explanation!
Claudio Redi