none
How to find the current system userId and Password in c#?

    Question

  • Hi,

    Hi in my application i want to check the given userid and password with the windows user id and password.
    How to find the current system userId and Password in c#?
    Any one send me code plz...

    Regards,
    venkat.

    venakt
    Friday, March 13, 2009 7:37 AM

Answers

  • Good point :}  What I meant was that switching from the LDAP provider (LDAP://) to the NTLM provider (WinNT://) might allow it to work.  Actually I just ran a test and switching to the NTLM provider will work for local users.  I don't know about global users on a domain (if you aren't running AD).  However you have to specify the machine name as the domain before a local user will authenticate properly.

    Michael Taylor - 3/13/09
    http://p3net.mvps.org
    • Marked as answer by Zhi-Xin Ye Thursday, March 19, 2009 10:14 AM
    Friday, March 13, 2009 1:39 PM

All replies

  •  http://www.codeproject.com/KB/system/everythingInAD.aspx#35
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Friday, March 13, 2009 10:34 AM
  • Just to point out you can't get the password of any user in the system.   Windows doesn't actually store the password.  Instead it stores a one-way hash of the password.  The password can't be retrieved from the hash.  If two passwords hash to the same value then they must match (at least mathematically). 

    You can get the currently logged in user's name in several different ways.  The easiest, IMHO, is to use WindowsIdentity.GetCurrent or Environment.UserName.

    If you aren't running AD then Lucian's code might not work for you.  You could try the WinNT provider instead but I don't know if it works in this scenario.  Alternatively you can use P/Invoke to attempt the logon directly.  The following MSDN topic discusses how to impersonate a user: http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx

    The important part is the logon portion.  If the logon is successful then the user is authenticated.  You can skip the impersonation step.  Just be sure to release the resource (CloseHandle) when you're done to ensure the user gets logged off.

    Michael Taylor - 3/13/09
    http://p3net.mvps.org
    Friday, March 13, 2009 1:27 PM
  • TaylorMichaelL said:



    If you aren't running AD then Lucian's code might not work for you. Michael Taylor - 3/13/09
    http://p3net.mvps.org



    if he's not running AD it will surely not work :)
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Friday, March 13, 2009 1:30 PM
  • Good point :}  What I meant was that switching from the LDAP provider (LDAP://) to the NTLM provider (WinNT://) might allow it to work.  Actually I just ran a test and switching to the NTLM provider will work for local users.  I don't know about global users on a domain (if you aren't running AD).  However you have to specify the machine name as the domain before a local user will authenticate properly.

    Michael Taylor - 3/13/09
    http://p3net.mvps.org
    • Marked as answer by Zhi-Xin Ye Thursday, March 19, 2009 10:14 AM
    Friday, March 13, 2009 1:39 PM