Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1024973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:51:25+00:00 2026-05-16T11:51:25+00:00

I am using C# and .Net Framework 4. I am looking for a foolproof

  • 0

I am using C# and .Net Framework 4.

I am looking for a foolproof method to get the login id of the currently logged in windows user that is not susceptible to impersonation or hacking. I am looking for this in the form of: DOMAINNAME\USERNAME

e.g. SOMEDOMAIN\JohnDoe

Currently the best I have is:

var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
var currentLoginId = identity.Name;

Is this open to impersonation (outside of someone knowing both the username and password) and if so is there a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:51:26+00:00Added an answer on May 16, 2026 at 11:51 am

    There can be at least four different identities involved at this point:

    1. The identity assigned to the thread
    2. The identity assigned to the process
    3. The account of the user who started the process
    4. The account of the user who logged onto the PC

    In your code you’re getting (1). This is normally fine, and is usually the same as (2).

    To retrieve (2), you could:

    1. Call WindowsIdentity.GetCurrent to get the impersonated identity
    2. Undo impersonation by calling the Win32 RevertToSelf function
    3. Look at WindowsIdentity.GetCurrent to get the underlying process identity
    4. Reset the thread identity by impersonating the identity from step (1)

    (2) and (3) will be the same unless you’ve written unmanaged code that changes the process identity. As @Daniel points out, (3) and (4) could legitimately be different in the presence of the Windows “run as” command.

    Edit: You can trust that the current WindowsIdentity is who it says it is, insofar as you can trust any given piece of data in your application.

    You could spoof it by attaching a debugger to your process and faking the return value from this call, but if you’re going to do that, you might as well fake the piece of code where you pass the user name to the database.

    The 100% safe way to do this is to use integrated authentication/SSPI to connect to the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a method which can fetch available time zones. Using .net
Looking for a control for selecting a directory in Windows Mobile (using .NET Compact
I'm using VB.NET on Framework 2.0. I'm looking to quickly group a Generic List<>
I am using C# and targeting the .NET Framework 3.5. I'm looking for a
I am looking into creating a new website using ASP.NET 4.0. I am currently
Using C# (the .NET framework), I'm looping through all the TimeZones and their AdjustmentRules...
I'm using .NET framework (tried 3.5 & 4.0) to load a .TIFF file and
I am developing a C# application using .NET Framework 4.0 in VS2010. It is
I'm programming a software using .NET FRAMEWORK 4.5 in METRO APPLICATIONS. I'm using the
What is difference in developing applications using .Net Framework, Asp.net and developing application in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.