CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005
    Posts
    172

    WindowsIdentity in C#

    Hello Everyone,

    I have a question regarding windows Identity.

    I am using the following code to get the user name of the user which is currently logged in the system

    Code:
     string pcLoggedInUserName, loggedInuserWithoutDomain = string.Empty;
     int indexOfFirstSlash;
     pcLoggedInUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
     indexOfFirstSlash = pcLoggedInUserName.IndexOf('\\');
     loggedInuserWithoutDomain = pcLoggedInUserName.Substring(indexOfFirstSlash + 1);
     return loggedInuserWithoutDomain;

    As you can see I need to get the user name only without the domain name. Right now the WindowsIdentity.GetCurrent().Name returns DOMAIN\\username and I need to get the user name only. I have managed to accomplish that by getting the substring.

    My question is this: Does the GetCurrent() function always return the username in the DOMAIN\\username format ( since my logic depends on that) or is it system speciffic?

    Thanks in advance

    Susan

  2. #2
    Join Date
    Nov 2008
    Posts
    15

    Re: WindowsIdentity in C#

    To get the username you could simply use System.Environment.UserName.

  3. #3
    Join Date
    Apr 2005
    Posts
    172

    Re: WindowsIdentity in C#

    Thanks so much for the fast reply. It has been very helpful.
    I implemented the suggestion and my solution looks much cleaner.

    Regards,

    Susan

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: WindowsIdentity in C#

    As far as I know, domain name is part of windows identity, so it is allways listed together with the plain user name.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured