static SafeToken LogonUserAndGetToken(string username, string password)
 {
     return(SafeToken.LogonUser(
                username,
                password,
                LOGON32_LOGON_NETWORK,
                LOGON32_PROVIDER_DEFAULT));
 }
 internal static extern bool LogonUser(
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszUsername,
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszDomain,
     IntPtr lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out SafeToken phToken
     );
        static bool IsAdmin(string userName, string password)
        {
            SafeToken       token    = Credentials.GetTokenFromCredentials(userName, password);
            WindowsIdentity identity = new WindowsIdentity(token.DangerousGetHandle());

            if (identity.IsSystem)
            {
                return(true);
            }

            WindowsPrincipal principal = new WindowsPrincipal(identity);

            if (principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                return(true);
            }

            return(false);
        }
        internal static int ValidateCredentials(string username, string password, bool throwException)
        {
            try
            {
                using (SafeToken token = LogonUserAndGetToken(username, password))
                {
                    return(0);
                }
            }
            catch (Win32Exception exception)
            {
                if (throwException)
                {
                    throw new System.Security.Authentication.AuthenticationException(exception.Message, exception);
                }

                return(exception.ErrorCode);
            }
        }
 internal static extern bool LogonUser(
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszUsername,
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszDomain,
     IntPtr lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out SafeToken phToken
 );