how to... First of all, we need to know what Active directory is. Here in this post, I am not going to provide information about Active Directory. Please see yourself about Active Directory or let me post about that later.
Its all simple to create a home page or any site displaying the user name without any input from user to provide, but all these are possible only for Intranet Websites only.
Things you require: Namespaces:
using System.DirectoryServices; using System.Security.Permissions;
You need to know your LDAP Path of your company active directory. Then add that LDAP Path in web.config file as
value="LDAP://domain-ip or address/DC=domain,DC=com"/>
Then in page_load() event, try this code. The code shown below will do the following: 1) Gets the LDAP path from web.config into a local variable.
String LDAPpath = System.Configuration.ConfigurationSettings.AppSettings["LDAPpath"];
2) Now identifying the windows identity credentials of the currently logged in user details.
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
3) The string array “a” will contain the domain name and the user name in which name the user is logged in.
string[] a = Context.User.Identity.Name.Split('\\');
4) Getting the instance of the directory entry from the user name and the domain name which is available from the string array “a”.
System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);
5) Getting the “full name” of the currently logged in user, using the property string of the directory entry.
string Name = ADEntry.Properties["FullName"].Value.ToString(); Response.Write("" + Name + "");
6) You can also look around the complete set of properties the directory entry has, by using this code.
foreach (string Key in ADEntry.Properties.PropertyNames) { string sPropertyValues = String.Empty; foreach (object Value in ADEntry.Properties[Key]) { sPropertyValues += Convert.ToString(Value) + ";"; } sPropertyValues = sPropertyValues.Substring(0, sPropertyValues.Length - 1); Response.Write("key:" + Key + "-Value:" + sPropertyValues); }
Conclusion This may help you show your required information on the page about the user who logged into that machine.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|