• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# Principal.WindowsImpersonationContext类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Security.Principal.WindowsImpersonationContext的典型用法代码示例。如果您正苦于以下问题:C# WindowsImpersonationContext类的具体用法?C# WindowsImpersonationContext怎么用?C# WindowsImpersonationContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



WindowsImpersonationContext类属于System.Security.Principal命名空间,在下文中一共展示了WindowsImpersonationContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: RevertToImpersonatedUser

 public static void RevertToImpersonatedUser(WindowsImpersonationContext _serviceAccountContext)
 {
     if (_serviceAccountContext != null)
     {
         _serviceAccountContext.Undo();
     }
 }
开发者ID:k2workflow,项目名称:K2Field.Helpers,代码行数:7,代码来源:WindowsPrincipalHelper.cs


示例2: ImpersonationData

 public ImpersonationData(IntPtr TokenHandle, IntPtr DupeTokenHandle, WindowsIdentity Identity, WindowsImpersonationContext ImpersonatedUser)
 {
     this.TokenHandle = TokenHandle;
     this.DupeTokenHandle = DupeTokenHandle;
     this.Identity = Identity;
     this.ImpersonatedUser = ImpersonatedUser;
 }
开发者ID:ethielg,项目名称:Impersonation,代码行数:7,代码来源:Impersonation.cs


示例3: Impersonate

        public void Impersonate()
        {
            Authenticated = false;

            // Remove the current impersonation by calling RevertToSelf()
            if (RevertToSelf())
            {
                Authenticated = LogonUserA(_username, _domain, _password,
                                    LOGON32_LOGON_INTERACTIVE,
                                    LOGON32_PROVIDER_DEFAULT,
                                    out _handle) != 0;

                if (!Authenticated)
                {
                    ErrorCode = Marshal.GetLastWin32Error();
                }

                // Make a copy of the token for the windows identity private member.
                if (DuplicateToken(this._handle, 2, out this._handleDuplicate) != 0)
                {
                    // set the private member for the current impersonation context.
                    this._context = WindowsIdentity.Impersonate(this._handleDuplicate.DangerousGetHandle());
                }
            }
        }
开发者ID:ricardocarneiro,项目名称:sharepoint-3,代码行数:25,代码来源:ImpersonationA.cs


示例4: Impersonate

        public void Impersonate(string domainName, string userName, string password)
        {
            try
            {
                const int logon32ProviderDefault = 0;
                const int logon32LogonInteractive = 2;
                TokenHandle = IntPtr.Zero;
                var returnValue = LogonUser(
                                    userName,
                                    domainName,
                                    password,
                                    logon32LogonInteractive,
                                    logon32ProviderDefault,
                                    ref TokenHandle);

                if (false == returnValue)
                {
                    int ret = Marshal.GetLastWin32Error();
                    Console.WriteLine("LogonUser call failed with error code : " + ret);
                    throw new System.ComponentModel.Win32Exception(ret);
                }
                NewId = new WindowsIdentity(TokenHandle);
                ImpersonatedUser = NewId.Impersonate();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred. " + ex.Message);
            }
        }
开发者ID:JonasSyrstad,项目名称:Stardust,代码行数:29,代码来源:ImpersonateUser.cs


示例5: Impersonate

 public void Impersonate(string domainName, string userName, string password){
     //try
     {
         // Use the unmanaged LogonUser function to get the user token for
         // the specified user, domain, and password.
         const int LOGON32_PROVIDER_DEFAULT = 0;
         // Passing this parameter causes LogonUser to create a primary token.
         const int LOGON32_LOGON_INTERACTIVE = 2;
         tokenHandle = IntPtr.Zero;
         // ---- Step - 1
         // Call LogonUser to obtain a handle to an access token.
         bool returnValue = LogonUser(
                                 userName,
                                 domainName,
                                 password,
                                 LOGON32_LOGON_INTERACTIVE,
                                 LOGON32_PROVIDER_DEFAULT,
                                 ref tokenHandle); // tokenHandle - new security token
         if (false == returnValue){
             int ret = Marshal.GetLastWin32Error();
             throw new System.ComponentModel.Win32Exception(ret);
         }
         // ---- Step - 2
         WindowsIdentity newId = new WindowsIdentity(tokenHandle);
         // ---- Step - 3
         {
             impersonatedUser = newId.Impersonate();
         }
     }
 }
开发者ID:MichelKansou,项目名称:RedXProject,代码行数:30,代码来源:Impersonate.cs


示例6: PerformanceCounterRetriever

        public PerformanceCounterRetriever(string server, string domain, string user, string password)
        {
            if (string.IsNullOrWhiteSpace(server))
                throw new ArgumentException($"Null/blank {nameof(server)} specified");
            if (string.IsNullOrWhiteSpace(domain))
                throw new ArgumentException($"Null/blank {nameof(domain)} specified");
            if (string.IsNullOrWhiteSpace(user))
                throw new ArgumentException($"Null/blank {nameof(user)} specified");
            if (password == null)
                throw new ArgumentNullException(nameof(password));

            try
            {
                var userHandle = new IntPtr(0);
                var logonSuccess = LogonUser(user, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, ref userHandle);
                if (!logonSuccess)
                    throw new Exception("LogonUser failed");
                _identity = new WindowsIdentity(userHandle);
                _context = _identity.Impersonate();
                _server = server;
                _disposed = false;
            }
            finally
            {
                Dispose();
            }
        }
开发者ID:zhangz,项目名称:Toolbox,代码行数:27,代码来源:PerformanceCounterRetriever.cs


示例7: Enter

        public void Enter()
        {
            if (this.IsInContext)
            {
                return;
            }

            this.token = new IntPtr(0);
            try
            {
                this.token = IntPtr.Zero;
                bool logonSuccessfull = NativeMethods.LogonUser(
                   this.username,
                   this.domain,
                   this.password,
                   Logon32LogonInteractive,
                   Logon32ProviderDefault,
                   ref this.token);
                if (logonSuccessfull == false)
                {
                    int error = Marshal.GetLastWin32Error();
                    throw new Win32Exception(error);
                }

                var identity = new WindowsIdentity(this.token);
                this.context = identity.Impersonate();
            }
            catch
            {
                // Catch exceptions here
            }
        }
开发者ID:romanpeshterski,项目名称:OpenJudgeSystem,代码行数:32,代码来源:WrapperImpersonationContext.cs


示例8: Enter

 public void Enter()
 {
     if (this.IsInContext) return;
     m_Token = new IntPtr(0);
     try
     {
         m_Token = IntPtr.Zero;
         bool logonSuccessfull = LogonUser(
            m_Username,
            m_Domain,
            m_Password,
            LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT,
            ref m_Token);
         if (logonSuccessfull == false)
         {
             int error = Marshal.GetLastWin32Error();
             throw new Win32Exception(error);
         }
         WindowsIdentity identity = new WindowsIdentity(m_Token);
         m_Context = identity.Impersonate();
     }
     catch (Exception exception)
     {
         // Catch exceptions here
     }
 }
开发者ID:nathantownsend,项目名称:myCoal,代码行数:27,代码来源:WrapperImpersonationContext.cs


示例9: Impersonate

 public void Impersonate()
 {
     //create identity
     WindowsIdentity newId = new WindowsIdentity(this.tokenHandle);
     //start impersonating
     this.impersonatedUser = newId.Impersonate();
 }
开发者ID:ipduncan,项目名称:Refactor,代码行数:7,代码来源:Impersonation.cs


示例10: WindowsImpersonatedIdentity

 /// <summary>
 ///   Constructor. Starts the impersonation with the given credentials. Please note that the account that instantiates the Impersonator class needs to have the 'Act as part of operating system' privilege set.
 /// </summary>
 /// <param name="userName"> The name of the user to act as. </param>
 /// <param name="domainName"> The domain name of the user to act as. </param>
 /// <param name="password"> The password of the user to act as. </param>
 public WindowsImpersonatedIdentity(string userName, string domainName, string password)
 {
     var token = IntPtr.Zero;
     var tokenDuplicate = IntPtr.Zero;
     try {
         if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(domainName) && string.IsNullOrEmpty(password)) {
             identity = WindowsIdentity.GetCurrent();
         } else {
             if (LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
                 if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
                     identity = new WindowsIdentity(tokenDuplicate);
                     impersonationContext = identity.Impersonate();
                 } else {
                     throw new Win32Exception(Marshal.GetLastWin32Error());
                 }
             } else {
                 throw new Win32Exception(Marshal.GetLastWin32Error());
             }
         }
     } finally {
         if (token != IntPtr.Zero) {
             CloseHandle(token);
         }
         if (tokenDuplicate != IntPtr.Zero) {
             CloseHandle(tokenDuplicate);
         }
     }
 }
开发者ID:virmitio,项目名称:coapp,代码行数:34,代码来源:Impersonation.cs


示例11: impersonateValidUser

        //public void Page_Load(Object s, EventArgs e)
        //{
        //    if (impersonateValidUser("username", "domain", "password"))
        //    {
        //        //Insert your code that runs under the security context of a specific user here.
        //        undoImpersonation();
        //    }
        //    else
        //    {
        //        //Your impersonation failed. Therefore, include a fail-safe mechanism here.
        //    }
        //}

        /// <summary>
        /// 模擬使用者
        /// </summary>
        /// <param name="userName">使用者名稱</param>
        /// <param name="domain">網域名稱</param>
        /// <param name="password">密碼</param>
        /// <returns>True:模擬成功;False:模擬失敗</returns>
        public static bool impersonateValidUser(String userName, 
                                                String domain, 
                                                String password
                                                )
        {
            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;
        }
开发者ID:ChiangHanLung,项目名称:PIC_VDS,代码行数:53,代码来源:Impersonate.cs


示例12: Impersonate

        /// <summary>
        /// 사용자를 가장합니다.
        /// </summary>
        /// <param name="userName">사용자 이름 입니다.</param>
        /// <param name="domain">도메인 이름 입니다.</param>
        /// <param name="password">암호 입니다.</param>
        /// /// <exception cref="SecurityException">가장이 실패할 경우 <c>SecurityException</c>를 던집니다.</exception>
        public void Impersonate(string userName, string domain, string password)
        {
            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;
                        }
                    }
                }
            }

            if (token != IntPtr.Zero) CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate);

            throw new SecurityException("사용자를 가장하는데 실패했습니다.");
        }
开发者ID:tomochandv,项目名称:Test,代码行数:37,代码来源:Identity.cs


示例13: Impersonation

        private Impersonation(string domain, string username, string password, LogonType logonType)
        {
            IntPtr handle;
            var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, 0, out handle);
            if (!ok)
            {
                var errorCode = Marshal.GetLastWin32Error();
                throw new ApplicationException(string.Format("Could not impersonate the elevated user.  LogonUser returned error code {0}.", errorCode));
            }

            var profileInfo = new ProfileInfo();
            profileInfo.dwSize = Marshal.SizeOf(profileInfo);
            profileInfo.lpUserName = username;
            profileInfo.dwFlags = 1;

            ok = NativeMethods.LoadUserProfile(handle, ref profileInfo);
            if (ok == false)
            {
                var errorCode = Marshal.GetLastWin32Error();
                throw new ApplicationException(string.Format("Could not load profile. Error code {0}.", errorCode));
            }
            NativeMethods.UnloadUserProfile(handle, profileInfo.hProfile);

            _handle = new SafeTokenHandle(handle);
            _context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle());
        }
开发者ID:jamezor,项目名称:SimpleImpersonation,代码行数:26,代码来源:Impersonation.cs


示例14: Impersonate

        public bool Impersonate(string userName, string domain, string password)
        {
            var token = IntPtr.Zero;
            var tokenDuplicate = IntPtr.Zero;

            if(RevertToSelf())
            {
                if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out token))
                {
                    if(DuplicateToken(token, 2, out tokenDuplicate) != 0)
                    {
                        var 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;
        }
开发者ID:Robin--,项目名称:Warewolf,代码行数:32,代码来源:Impersonator.cs


示例15: Impersonate

        public void Impersonate()
        {
            // Create the new Identity from the token
            WindowsIdentity impersonatedID = new WindowsIdentity(this._tokenHandle);

            // Start impersonating as the new ID
            this._impersonatedUser = impersonatedID.Impersonate();
        }
开发者ID:zoso10,项目名称:ErrorLogger,代码行数:8,代码来源:Impersonator.cs


示例16: Dispose

		/// <summary>
		/// Ends impersonation of the WCF client's Windows credentials.
		/// </summary>
		public void Dispose()
		{
			if (_windowsImpersonationContext != null)
			{
				_windowsImpersonationContext.Dispose();
				_windowsImpersonationContext = null;
			}
		}
开发者ID:nhannd,项目名称:Xian,代码行数:11,代码来源:ServiceClientImpersonationContext.cs


示例17: Impersonation

 /// <summary>
 /// Constructor begins impersonation based on user credentials passed in. 
 /// </summary>
 /// <param name="domain">Windows Domain</param>
 /// <param name="userName">Windows username</param>
 /// <param name="password">Windows password</param>
 public Impersonation(string domain, string userName, string password)
 {
     if (!string.IsNullOrEmpty(password))
     {
         _wc = WindowsIdentity.GetCurrent().Impersonate();
         ImpersonateValidUser(domain, userName, password);
     }
 }
开发者ID:dbremner,项目名称:Impersonator,代码行数:14,代码来源:Impersonation.cs


示例18: Impersonate

 /// <summary>
 /// Starts the impersonation.
 /// </summary>
 public void Impersonate()
 {
     // Create Identity.
     WindowsIdentity newId = new WindowsIdentity(this.tokenHandle);
     
     // Start impersonating.
     this.impersonatedUser = newId.Impersonate();
 }
开发者ID:BiYiTuan,项目名称:CruiseControl.NET,代码行数:11,代码来源:Impersonation.cs


示例19: Impersonation

        /// <summary>
        /// Initializes a new instance of the <see cref="Impersonation"/> class.
        /// </summary>
        /// <param name="windowsId">The Windows ID.</param>
        /// <exception cref="ArgumentNullException" />
        public Impersonation(WindowsIdentity windowsId)
        {
            if (windowsId == null)
            {
                throw new ArgumentNullException("windowsId");
            }

            this.windowsImpersonationContext = windowsId.Impersonate();
        }
开发者ID:StealFocus,项目名称:BclExtensions,代码行数:14,代码来源:Impersonation.cs


示例20: Impersonate

 public void Impersonate()
 {
     // authenticates the domain user account and begins impersonating it 
     WindowsIdentity temp = this.Logon();
     if (temp != null)
     {
         this.impersonationContext = temp.Impersonate();
     }
 }
开发者ID:amalapannuru,项目名称:RFC,代码行数:9,代码来源:Impersonator.cs



注:本文中的System.Security.Principal.WindowsImpersonationContext类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Principal.WindowsPrincipal类代码示例发布时间:2022-05-26
下一篇:
C# Principal.WindowsIdentity类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap