WinForms:
1: using System.Security.Principal;
2: ... 3: ... 4: WindowsIdentity identity = WindowsIdentity.GetCurrent();5: string userName = identity.Name;
Web:
1: using System.Security.Principal;
2: ... 3: ... 4: 5: IPrincipal userPrincipal = HttpContext.Current.User;6: WindowsIdentity identity = userPrincipal.Identity as WindowsIdentity;
7: 8: string userName = identity.Name;
Tip #2: Como imprimir los atributos y sus valores (getters/setters) de una clase usando Reflection:
1: /// <summary>
2: /// Despliega los atributos de una clase y su valor
3: /// </summary>
4: private void desplegarAtributos(object clase)
5: {6: if(clase != null)
7: { 8: Type objectType = clase.GetType(); 9: PropertyInfo[] propertyInfo = objectType.GetProperties(); 10: 11: Console.WriteLine();12: Console.WriteLine("\tNombre de la clase: {0}", clase);
13: 14: Console.WriteLine();15: foreach (var info in propertyInfo)
16: {17: object foo = info.GetValue(clase, null);
18: Console.WriteLine("\t\t{0} = {1}", info.Name, foo);
19: } 20: } 21: }
