diff --git a/Src/NCCommon/Util/SecurityUtil.cs b/Src/NCCommon/Util/SecurityUtil.cs index 3c6c3e62..2fa8535b 100644 --- a/Src/NCCommon/Util/SecurityUtil.cs +++ b/Src/NCCommon/Util/SecurityUtil.cs @@ -31,20 +31,32 @@ static extern bool LogonUser( public static bool VerifyWindowsUserForRole(string nodeName, string userName, string password, WindowsBuiltInRole role) { bool isAdministrator = false; - IntPtr token; + IntPtr token = IntPtr.Zero; try { - LogonUser(userName, nodeName, password, 3, 0, out token); - WindowsIdentity identity = new WindowsIdentity(token); - WindowsPrincipal principal = new WindowsPrincipal(identity); - if (principal.IsInRole(role)) + if (!LogonUser(userName, nodeName, password, 3, 0, out token)) { - isAdministrator = true; + return false; + } + using (WindowsIdentity identity = new WindowsIdentity(token)) + { + WindowsPrincipal principal = new WindowsPrincipal(identity); + if (principal.IsInRole(role)) + { + isAdministrator = true; + } } } - catch (Exception ex) + catch { - + return false; + } + finally + { + if (token != IntPtr.Zero) + { + Marshal.Release(token); + } } return isAdministrator; }