步驟:
1. 引用System.Security.Principal命名空间
using System.Security.Principal;
2. 代码
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
另外:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
//Get the current principal and put it into a principal object.
WindowsPrincipal MyPrincipal = (Thread.CurrentPrincipal as WindowsPrincipal);
//Check the name and see if the user is authenticated.
if(MyPrincipal.Identity.Name.Equals(@"XFRAME\Zeng.Zeng") && MyPrincipal.Identity.IsAuthenticated.Equals(true))
{
MessageBox.Show("Hello "+MyPrincipal.Identity.Name.ToString()+", you are authenticated!" );
}
else
{
MessageBox.Show("Go away! You are not authorized!");
}