Monday, March 30, 2009

Get Windows Account For ASP.Net 2.0

Under ASP.Net 2.0, if you choose windows Authentication
1. In Web.config, set Authentication mode="Windows"
2. In IIS, set the security
2.1 Clear Anonymous
2.2 Choose Integrated Windows Authentication or
2.3 Use Client Certification and Map it to a windows account

Then in the Code, Get the windows Account as below

1. WindowsIdentity myIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(myIdentity);
The myPrincipal is the current account that ASP.Net running on

2. HttpContext.Current.Request.LogonUserIdentity
This is the account for the current visit account

3. HttpContext.Current.User.Identity and Thread.CurrentPrincipal.Identity are always Emtpy!