Exemplo n.º 1
0
        private static WindowsLogonResult AttemptLogon(NetworkCredential credentials)
        {
            if (credentials == null)
            {
                return(WindowsLogonResult.Invalid());
            }

            SafeAccessTokenHandle safeAccessTokenHandle;

            var success = NativeMethods.LogonUser(
                credentials.UserName,
                credentials.Domain,
                credentials.Password,
                Win32Logon.LOGON_INTERACTIVE,
                Win32Logon.PROVIDER_DEFAULT,
                out safeAccessTokenHandle);

            var result = new WindowsLogonResult {
                Success = success, Token = safeAccessTokenHandle
            };

            if (success)
            {
                result.Message = "Success";
            }
            else
            {
                var ex = new Win32Exception(Marshal.GetLastWin32Error());

                result.Exception = ExceptionDispatchInfo.Capture(ex);
                result.Message   = ex.Message;
            }

            return(result);
        }
Exemplo n.º 2
0
 public static WindowsLogonResult Logon(NetworkCredential credentials)
 {
     try
     {
         return(AttemptLogon(credentials));
     }
     catch (Exception ex)
     {
         return(WindowsLogonResult.Error(ex));
     }
 }