0

I have a website that uses an app pool with a service ID to connect to a database. I want to be able to populate a textbox on the web page that shows their actual windows log in name and not the service ID that is associated with the app pool. I want this so I can use this information to do some filtering on a gridview that shows some data from the database based on their username. Currently I have tried a couple different ways.

Environment.UserName

This gets me the service ID that is being used to access the database.

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

This also gets me the service ID that is being used to access the database.

//Both of these return the same results described below.
HttpContext.Current.User.Identity.Name;
//and
System.Threading.Thread.CurrentPrincipal.Identity.Name;

These two are kind of weird. They get my username when I debug through vs2010 but when I copy the files to the server this will run on and have it using the app pool it gets me nothing. I have it set up to show in a textbox on the home.aspx page.

Any help on this would be greatly appreciated as I have looked everywhere and cant find any information that is specific to my situation.

Thanks in advance!!

Actual code currently being used: (returns nothing when run from server)

if (!IsPostBack)
        {
            string filterName = HttpContext.Current.User.Identity.Name;
            IDFilterTextBox.Text = filterName;
        }

Edited: to include a new method I tried.

SteveAnselment
  • 634
  • 3
  • 10
  • 29

2 Answers2

0

I think what you are attempting is not possible. The reason it sees you when debugging is because VS is running the site, and running under your username.

When you deploy the app to a server, the code running there has no way of accessing the user's machine - so cannot find the username.

You could run some Javascript on the user's machine, but I don't think it's possible to get the local machine username from that.

UPDATE: Okay, if the user is logged in to the site somehow then that's different - I misunderstood the question. You can try getting it from:

System.Threading.Thread.CurrentPrincipal.Identity.Name

Fordio
  • 3,410
  • 2
  • 14
  • 18
  • Then why would it use HttpContext? Seems to me that it would work just fine... I don't see why i cant get this info out of it. By the way, this is an intranet site. it will only be available to users that are at my company. They have to have access through active directory and that is ultimately the username I am looking for. If anyone has another way to get this I would appreciate the help!!! – SteveAnselment Oct 01 '14 at 22:30
  • Didn't realize users were logged into your site. See my update. – Fordio Oct 02 '14 at 07:19
  • Tried the System.Threading.Thread.CurrentPrincipal.Identity.Name way and it still returns an empty string when I am running it from the server. There has to be a way to get a users User Name prior to the Service ID authenticating. I am really stuck on this and am willing to entertain alternate approaches to this as well. – SteveAnselment Oct 02 '14 at 15:41
  • I'm sorry but I really think the site does not know about the user. Yes you may have to be logged in through AD to get to it, but that probably just opens a route on the network so you can get to the url. I would check this with your network guys. – Fordio Oct 02 '14 at 15:51
0

So this turned out to be a simple fix using one of the methods I had shown before.

string UserName = System.Threading.Thread.CurrentPrincipal.Identity.Name;

The tricky part was in the IIS Configuration. Once we figured out that Anonymous Authentication being disabled forces the Users log in ID to come accross It worked fine.

SteveAnselment
  • 634
  • 3
  • 10
  • 29