コード例 #1
0
 static SafeToken LogonUserAndGetToken(string username, string password)
 {
     return(SafeToken.LogonUser(
                username,
                password,
                LOGON32_LOGON_NETWORK,
                LOGON32_PROVIDER_DEFAULT));
 }
コード例 #2
0
 internal static extern bool LogonUser(
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszUsername,
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszDomain,
     IntPtr lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out SafeToken phToken
     );
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
 internal static extern bool LogonUser(
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszUsername,
     [MarshalAs(UnmanagedType.LPTStr)]
     string lpszDomain,
     IntPtr lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out SafeToken phToken
 );