C#模拟域登录

如果希望使用C#进行后台域登录,需要使用到advapi32.dll这个程序集。advapi32.dll是一个高级API应用程序接口服务库的一部分,包含的函数与对象的安全性,注册表的操控以及事件日志有关。xp系统一般位于C:\WINDOWS\system32\目录下,大小659KB。

下面是实现域登录的代码:

public class SimulateDomainService
    {
        public static bool ImpersonateValidUser(String userName, String domain, String password)
        {
            //ServiceContext.SetThreadPrincipal();
            WindowsImpersonationContext impersonationContext;

            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }
        /// <summary>
        /// 模拟登录
        /// </summary>
        /// <returns></returns>
        public static bool ImpersonateValidUser()
        {
            return ImpersonateValidUser(GetValue("account"), GetValue("domain"), GetValue("pwd"));
        }
        /// <summary>   
        /// 读取指定key的值   
        /// </summary>   
        /// <param name="key"></param>   
        /// <returns></returns>   
        public static string GetValue(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key].ToString();
        }

        public static CustomReportCredentials GetReportCredentials(String account,String domain,String pwd)
        {
            return new CustomReportCredentials(account, pwd, domain);
        }
        /// <summary>
        /// 获取报表认证信息
        /// </summary>
        /// <returns></returns>
        public static CustomReportCredentials GetReportCredentials()
        {
            return GetReportCredentials(GetValue("account"), GetValue("domain"), GetValue("pwd"));
        }
        #region win32 api extend method
        [DllImport("advapi32.dll")]
        static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        static extern bool CloseHandle(IntPtr handle);

        const int LOGON32_LOGON_INTERACTIVE = 2;
        const int LOGON32_PROVIDER_DEFAULT = 0;
        #endregion


    }

其中:
ImpersonateValidUser
这个方法有三个参数,分别是账户名、域名、密码。返回值是boole型,如果返回true则说明登录成功,否则登录失败。

注意:服务器需要在模拟登录的域名之内(属于同一域)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值