public bool isKerberos() { if (HAP.Web.Configuration.hapConfig.Current.AD.AuthenticationMode == Web.Configuration.AuthMode.Forms) { try { TokenGenerator.ConvertToPlain(HttpContext.Current.Request.Cookies["token"].Value); } catch { return(true); } } return(false); }
public bool ImpersonateContained() { if (HAP.Web.Configuration.hapConfig.Current.AD.AuthenticationMode == Web.Configuration.AuthMode.Forms) { if (string.IsNullOrEmpty(this.Password) && HttpContext.Current.Request.Cookies["token"] != null) { try { this.Password = TokenGenerator.ConvertToPlain(HttpContext.Current.Request.Cookies["token"].Value); } catch { this.Password = HttpContext.Current.Request.Cookies["token"].Value; return(false); } } } if (HAP.Web.Configuration.hapConfig.Current.AD.AuthenticationMode == Web.Configuration.AuthMode.Windows || this.Password == "kerberos") { if (ADUtils.RevertToSelf()) { ContainedImpersonationContext = ((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate(); return(true); } return(false); } WindowsIdentity tempWindowsIdentity; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; if (ADUtils.RevertToSelf()) { if (ADUtils.LogonUserA(this.UserName, this.DomainName, this.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (ADUtils.DuplicateToken(token, 2, ref tokenDuplicate) != 0) { tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); ContainedImpersonationContext = tempWindowsIdentity.Impersonate(); if (ContainedImpersonationContext != null) { ADUtils.CloseHandle(token); ADUtils.CloseHandle(tokenDuplicate); return(true); } } } else { throw new Exception("Something is wrong"); } } if (token != IntPtr.Zero) { ADUtils.CloseHandle(token); } if (tokenDuplicate != IntPtr.Zero) { ADUtils.CloseHandle(tokenDuplicate); } return(false); }
public bool Impersonate() { if (HAP.Web.Configuration.hapConfig.Current.AD.AuthenticationMode == Web.Configuration.AuthMode.Forms) { if (string.IsNullOrEmpty(this.Password) && HttpContext.Current.Request.Cookies["token"] != null) { try { this.Password = TokenGenerator.ConvertToPlain(HttpContext.Current.Request.Cookies["token"].Value); } catch { this.Password = HttpContext.Current.Request.Cookies["token"].Value; return(false); } } } if (HAP.Web.Configuration.hapConfig.Current.AD.AuthenticationMode == Web.Configuration.AuthMode.Windows) { if (ADUtils.RevertToSelf()) { ContainedImpersonationContext = ((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate(); return(true); } return(false); } WindowsIdentity tempWindowsIdentity; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; if (ADUtils.RevertToSelf()) { if (string.IsNullOrEmpty(this.Password) && HttpContext.Current.Request.Cookies["token"] == null) { FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage("error=timeout"); } else { if (ADUtils.LogonUserA(this.UserName, this.DomainName, this.Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (ADUtils.DuplicateToken(token, 2, ref tokenDuplicate) != 0) { tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); impersonationContext = tempWindowsIdentity.Impersonate(); if (impersonationContext != null) { ADUtils.CloseHandle(token); ADUtils.CloseHandle(tokenDuplicate); return(true); } } } else { throw new Exception("I cannot impersonate " + this.UserName + " due to an issue logging onto the domain " + this.DomainName + " using an Interactive Login. HAP+ Requires Interactive Login Rights on the Server it is running on"); } } } if (token != IntPtr.Zero) { ADUtils.CloseHandle(token); } if (tokenDuplicate != IntPtr.Zero) { ADUtils.CloseHandle(tokenDuplicate); } return(false); }