class1.cs

来自「C#开发教程 由浅入深 配有实例 是初学者的好帮手」· CS 代码 · 共 48 行

CS
48
字号
using System; 
using System.Threading; 
using System.Security.Principal; 

using System.Runtime.InteropServices;

namespace UseRBS_Impersonate
{
    public class UseRBS_ImpersonateApp 
    { 
        [DllImport("advapi32.dll")]
        public static extern bool LogonUser(
            string lpszUsername, string lpszDomain, string lpszPassword, 
            int dwLogonType, int dwLogonProvider, out int phToken);

        public static void Main(string[] args) 
        { 
            try
            {
                int token1 = 0;
                bool loggedOn = LogonUser(
                    "Bill", "VENUS", "banana", 3, 0, out token1);
                IntPtr token2 = new IntPtr(token1);
                WindowsIdentity wi = new WindowsIdentity(token2);
                WindowsImpersonationContext wic = 
                    wi.Impersonate();
 
                Console.WriteLine("Principal/Identity values:");
                Console.WriteLine("Name: " +wi.Name); 
                Console.WriteLine("AuthenticationType: " 
                    +wi.AuthenticationType); 
                Console.WriteLine("IsAuthenticated: " +wi.IsAuthenticated); 
                Console.WriteLine("IsAnonymous: " +wi.IsAnonymous); 
                Console.WriteLine("IsGuest: " +wi.IsGuest); 
                Console.WriteLine("IsSystem: " +wi.IsSystem); 
                Console.WriteLine("Token: " +wi.Token); 

                wic.Undo();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        } 
    } 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?