NT Login

P

Phil Barber

Using the Win32's I used "WNetGetUser" (winsock API ) to get the NT login
name.
what is the best way to achive this in C#?
thanks
Phil.
 
C

Carl Prothman [MVP]

Phil said:
Using the Win32's I used "WNetGetUser" (winsock API ) to get the NT
login name.
what is the best way to achive this in C#?
thanks

Phil,
Use Windows authentication and set Impersonate to true in your web.config file
e.g.
<authentication mode="Windows" />
<identity impersonate="true" />

Then use the Security Proincipal WindowsIdentity's GetCurrent method to get
the currenty authenticated user.
e.g.
private void Button1_Click(object sender, System.EventArgs e)
{
// Display the current Windows Identity
TextBox1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
C

Carl Prothman [MVP]

Khan said:
This Should be easier
string loginid=User.Identity.Name;

Sure it's easier to type in, but watch out, depending on the IIS / web.config file settings,
User.Identity.Name will not give you the correct result (for Windows authentication mode)

On a Windows Server 2003 box where I'm logged in as Administrator:

- With Identity Impersonate= false and IIS Anonymous Access enabled / NTLM enabled
=> WindowsIdentity.GetCurrent().Name = "NT AUTHORITY\NETWORK SERVICE"
=> User.Identity.Name = ""

- With Identity Impersonate= false and IIS Anonymous Access disabled / NTLM enabled
=> WindowsIdentity.GetCurrent().Name = "NT AUTHORITY\NETWORK SERVICE"
=> User.Identity.Name = "CARL20\Administrator"


- With Identity Impersonate= true and IIS Anonymous Access enabled / NTLM enabled
=> WindowsIdentity.GetCurrent().Name = "CARL20\IUSR_CARL20"
=> User.Identity.Name = ""

- With Identity Impersonate= true and IIS Anonymous Access disabled / NTLM enabled
=> WindowsIdentity.GetCurrent().Name = "CARL20\Administrator"
=> User.Identity.Name = "CARL20\Administrator"

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP

Hire top-notch developers at
http://www.able-consulting.com
 

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

Top