How can I get the domain name and username?

Discussion in 'ASP .Net Security' started by Cherry Adriano, Dec 11, 2003.

  1. I'm new with .net programming....so pls bear with me. May
    I ask how can I get the domain name and username of user
    who is trying access my web application.
    Cherry Adriano, Dec 11, 2003
    #1
    1. Advertising

  2. System.Web.HttpContext.User.Identity.Name will return a string in the form
    domain\userName.

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "Cherry Adriano" <> wrote in message
    news:095801c3bfb7$eebeade0$...
    > I'm new with .net programming....so pls bear with me. May
    > I ask how can I get the domain name and username of user
    > who is trying access my web application.
    Chris Jackson, Dec 11, 2003
    #2
    1. Advertising

  3. Hello Cherry,
    Add this to your web.config file. This file is in the solution files in
    Visual Studio .NET.
    <identity impersonate="true" />

    Then you can use this line of code.
    System.Security.Principal.WindowsIdentity.GetCurrent().Name)

    I hope this helps you.

    Best regards,
    John Soulis
    Microsoft, ASP.NET

    This posting is provided "AS IS", with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm
    John Soulis [MSFT], Dec 11, 2003
    #3
  4. Cherry Adriano

    Guest

    Sir Chris,
    System.Web.HttpContext.User.Identity.Name returns
    domain\username where domain is my computer name and
    username as ASPNET. Do you know how can I get network
    logon name(username) and domain used in logging on to
    Operating System?

    There is a way to get the network logon name using vb6 .
    See VB6 script below. How can I get this in VB.net Thanks
    =)

    Private Declare Function w32_WNetGetUser Lib "mpr.dll"
    Alias "WNetGetUserA" (ByVal lpszLocalName As String, ByVal
    lpszUserName As String, lpcchBuffer As Long) As Long
    Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail:
    Dim lpUserName As String, lpnLength As Long, lResult
    As Long
    'Create a buffer
    lpUserName = String(256, Chr$(0))
    'Get the network user
    lResult = w32_WNetGetUser(vbNullString, lpUserName,
    256)
    If lResult = 0 Then
    lpUserName = Left$(lpUserName, InStr(1,
    lpUserName, Chr$(0)) - 1)
    MsgBox "The user's Network Logon Name is " +
    lpUserName + ".", vbInformation + vbOKOnly, App.Title
    Else
    MsgBox "No user found !", vbExclamation +
    vbOKOnly, App.Title
    End If
    End Sub
    >-----Original Message-----
    >System.Web.HttpContext.User.Identity.Name will return a

    string in the form
    >domain\userName.
    >
    >--
    >Chris Jackson
    >Software Engineer
    >Microsoft MVP - Windows Client
    >Windows XP Associate Expert
    >--
    >More people read the newsgroups than read my email.
    >Reply to the newsgroup for a faster response.
    >(Control-G using Outlook Express)
    >--
    >
    >"Cherry Adriano" <> wrote in

    message
    >news:095801c3bfb7$eebeade0$...
    >> I'm new with .net programming....so pls bear with me.

    May
    >> I ask how can I get the domain name and username of user
    >> who is trying access my web application.

    >
    >
    >.
    >
    , Dec 16, 2003
    #4
  5. Sir Chris,
    System.Web.HttpContext.User.Identity.Name returns domain\username where
    domain is my computer name and username as ASPNET. Do you know how can I
    get network logon name(username) and domain used in logging on to
    Operating System?

    There is a way to get the network logon name using vb6 . See VB6 script
    below. How can I get this in VB.net Thanks =)

    Private Declare Function w32_WNetGetUser Lib "mpr.dll" Alias
    "WNetGetUserA" (ByVal lpszLocalName As String, ByVal lpszUserName As
    String, lpcchBuffer As Long) As Long
    Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail:
    Dim lpUserName As String, lpnLength As Long, lResult As Long
    'Create a buffer
    lpUserName = String(256, Chr$(0))
    'Get the network user
    lResult = w32_WNetGetUser(vbNullString, lpUserName, 256)
    If lResult = 0 Then
    lpUserName = Left$(lpUserName, InStr(1, lpUserName, Chr$(0)) -
    1)
    MsgBox "The user's Network Logon Name is " + lpUserName + ".",
    vbInformation + vbOKOnly, App.Title
    Else
    MsgBox "No user found !", vbExclamation + vbOKOnly, App.Title
    End If
    End Sub


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Cherry Adriano, Dec 16, 2003
    #5
  6. Hello Cherry,
    Did you add <identity impersonate="true" /> this to the web.config file and
    you need to have windows integrated select too in the directory security of
    your application root.
    Thank you,
    John Soulis
    Microsoft, ASP.NET

    This posting is provided "AS IS", with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm
    John Soulis [MSFT], Dec 16, 2003
    #6
  7. > Sir Chris,

    NICE! I have been knighted!

    John's answer is absolutely correct, this pulls the user token that is being
    used by the process for external calls. You have to have BOTH ASP.NET and
    IIS configured for Windows Authentication. The complete identity matrix can
    be found here:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetAP05.asp

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --
    Chris Jackson, Dec 16, 2003
    #7
  8. Cherry Adriano

    Cherry Guest

    I already added <identity impersonate="true" /> to the Web.Config file, however, error similar below was encountered

    "An error occurred while try to load the string resources (GetModuleHandle failed with error 126)."

    Please help. thanks =)
    Cherry, Jan 19, 2004
    #8
  9. Grant ASPNET account "Impersonate a client after authentication" privilege.

    Thank you,
    John Soulis
    Microsoft, ASP.NET

    This posting is provided "AS IS", with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm
    John Soulis [MSFT], Jan 20, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    1
    Views:
    577
    Pedro C. Valenzuela
    Aug 30, 2003
  2. Allister
    Replies:
    5
    Views:
    425
    Joe Kaplan \(MVP - ADSI\)
    Apr 4, 2006
  3. Shailesh Patel
    Replies:
    0
    Views:
    448
    Shailesh Patel
    Nov 8, 2006
  4. jach

    Get Username of a domain user

    jach, Jun 23, 2004, in forum: ASP General
    Replies:
    1
    Views:
    147
  5. Mav
    Replies:
    5
    Views:
    167
Loading...

Share This Page