views:

268

answers:

3

How can an application, running on a production server, access the login username of the machine that a user is accessing an application from? For example, I am currently logged into my machine on the INTRA corporate intranet. My username will be INTRA\Username.

I have added specific usernames to a database and wish to check this intranet username against the database to restrict access to an application and leverage the username across the application.

Currently, I am using the following code to access the username:

 Private username As String = Thread.CurrentPrincipal.Identity.Name

This is working great on localhost, but when authenticating against the database on a development server, I'm getting the following error:

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

Is this an incorrect approach? Is this even possible, or is it too much of a security issue? This application will be an internal intranet application running in an IE shop. Relevant pieces of web.config that already exist include:

 <identity impersonate="true"/>
 <authentication mode="Windows"/>
 <authorization>
  <deny users="?"/>
 </authorization>

<connectionStrings>
 <add name="CONNSTR" connectionString="Initial Catalog=DATANAME;Data Source=servername;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
+1  A: 

When setting up your web application on the server, you need to go into the Document Security section (the name of it changes depending on what version of IIS your server is running, but it's something like that), turn off anonymous authentication, and turn on Windows authentication. That tells the server to request windows login authentication from the browser. (Perhaps someone who knows web.config files better than I [which is nearly anyone] can edit this to point to the relevant bit; I don't think it's impersonate but if I knew, I'd say. I've so far only done this via the UI.)

T.J. Crowder
Unfortunately, this did not change my error. It does seem reasonable, however, and probably would have given me trouble later on. Thank you
4501
A: 

in your example, you are locating the username that your webserver is running under. What you are after is the username of the user accessing the page.

Try something like this:

Colin Pickard
I have read this article through and do not see anything that I have not attempted that would solve this. Is there a specific portion that you are referring to?
4501
I was thinking it might be something like `<authentication mode="Windows"/><identity impersonate="true"/>`. But reading your updates, it looks like you are already doing this. Sorry I couldn't be more help :(
Colin Pickard
A: 

If setting the directory security to Windows Authentication is not working, change it to Basic Authentication. You'll also need to specify the domain name to authenticate against. This was the only way we could get the security to propagate through from the IIS layer to the DB. Unfortunately this causes the username and password to be sent through clear text. Its not the best solution, but since things were on the Intranet, it worked while we work on updating our login procedure.

Dillie-O
This solution has relevance, but is far from ideal. I'm simply looking to read the information from the logged-in machine. I do not wish the user to have to type in a username or password.
4501