How can I get the domain name and username?

C

Cherry Adriano

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.
 
C

Chris Jackson

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

John Soulis [MSFT]

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
 
C

cherry_adriano

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: (e-mail address removed)
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
 
C

Cherry Adriano

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: (e-mail address removed)
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
 
J

John Soulis [MSFT]

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
 
C

Cherry

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 =)
 
J

John Soulis [MSFT]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top