.NET : 取得Windows账号的有关信息
下面这个小程序演示了如何取得当前用户身份的相关信息
using System; using System.Security.Principal; namespace ConsoleApplication1 { class Program { /// <summary> /// 这个程序演示了如何获得当前Windows账号的相关信息 /// 作者:陈希章 /// </summary> /// <param name="args"></param> static void Main(string[] args) { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsIdentity identity = System.Threading.Thread.CurrentPrincipal.Identity as WindowsIdentity; Console.WriteLine("SID:{0}",identity.User.ToString()); Console.WriteLine("Name:{0}",identity.Name); Console.WriteLine("IsSystem:{0}",identity.IsSystem); Console.WriteLine("AuthenticationType:{0}",identity.AuthenticationType); Console.WriteLine("Groups:{0}", identity.Groups.Count); Console.WriteLine("Group SID:"); foreach (var item in identity.Groups) { Console.WriteLine("\t{0}",item.Value); } Console.WriteLine("Group Name:"); foreach (var item in identity.Groups.Translate(typeof(NTAccount))) { Console.WriteLine("\t{0}",item.Value); } Console.Read(); } } }