If you enable ASP.Net impersonation in IIS, you can get the username as you like. This will only work if this data is in the membership provider in forms / AD and they are not anonymous.
In addition, mixing auth-based forms and Windows / AD is performance-based, but not recommended. See this one if you need to do this.
EDIT . I think I misunderstood what you wanted, so here comes a deep understanding of what is happening with the above solution:
If you turn off anonymous authentication and enable impersonation of Asp.Net, IIS will call 401 when someone visits the site.
If everything is in the same domain, the web browser will send your credentials to IIS, IIS will check them against it in Active Directory, and then AD will provide IIS Identity to work.
When you enable Asp.Net impersonation, IIS will then bind this Identity to the current thread / request. Therefore, after authentication, you can simply take the username from the current stream identifier, and then query Active Directory as:
using System.Threading; using System.DirectoryServices; using System.DirectoryServices.AccountManagement; ...... PrincipalContext pc = null; UserPrincipal principal = null; try { var username = Thread.CurrentPrincipal.Identity.Name; pc = new PrincipalContext(ContextType.Domain, "active.directory.domain.com"); principal = UserPrincipal.FindByIdentity(pc, username); var firstName = principal.GivenName ?? string.Empty var lastName = principal.Surname ?? string.Empty return string.Format("Hello {0} {1}!", firstName, lastName); } catch ... finally { if (principal != null) principal.Dispose(); if (pc != null) pc.Dispose(); }
source share