3

I have a ASP.NET application where an employee logs into Windows and then they have to select their name from a long list of names and log in into the application.

What I want to do is as soon as the employee opens the application it selects their name from the drop down box.

I believe if I can get the Windows User Id on page load I can then make my code select the user depending on the Windows user that is logged in.

Question is. How do you get Windows User Id that is currently logged in in C# Asp.net? Any ideas.

Apollo
  • 1,990
  • 12
  • 44
  • 65
  • 1
    What do you mean "windows user ID"? Microsoft account (something like "user@live.com"), domain user ( "mycompany\user"), local machine account ("mycomputer\mylocaluser") or even something else? (If asking for domain user - Rahul's answer have necessary bits, mostly in comments) – Alexei Levenkov Aug 26 '13 at 21:15
  • @AlexeiLevenkov I need the Domain User – Apollo Aug 26 '13 at 21:16
  • Probably off topic, but why the user needs to select from a list anyway? With Integrated Windows Authentication, your asp.net app can identify the user already. What if user pick someone else name to login? – Ray Cheng Aug 26 '13 at 21:45
  • @RayCheng the way it is now they select their name and input their password. I want them to skip that login process and log them in by domain user. – Apollo Aug 27 '13 at 13:29

3 Answers3

9

Try this:-

System.Web.HttpContext.Current.User.Identity.Name

Also check this forum.

UPDATE:-

1) Please check that you have disabled anonymous mode in IIS

2) Also do check that you have set the application to autheticate the user details like this:-

 <authentication mode="Windows"/> 
    <authorization> 
       <allow users="*"/>
    </authorization>

or like this:-

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

3) You can you can either configure IIS in Control Panel so that your site (or machine) uses Windows authentication and denies anonymous access

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

I was having an related issue where my local build was recognizing the WindowsIdentity.GetCurrent().Name.ToString() value as my Windows account name, but when I accessed the website via a server it was always "DefaultAppPool".

I was able to get this to work by doing the following:

  1. Open IIS and navigate to my website.
  2. Under the IIS sections, double click "Authentication".
  3. From there, disable "Anonymous Authentication" and "Forms Authentication".
  4. Enable "Windows Authentication".

From there I was able to discern the user id/name and proceed with my coding. I had to rework a little bit, but it was well worth it. Thanks for the advice!

GoCowboys
  • 31
  • 1
-1

In ASP.NET Core .NET 6, I use

Environment.UserName;

// or // the following only work on Windows platform

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

Sinji Yang
  • 89
  • 2