public override void Execute(object context, ActivityQueue queue, Trigger trigger) { // get the login information out of the credential store int count; IntPtr pCredentials; var ret = AdvApi32.CredEnumerate(null, 0, out count, out pCredentials); if (!ret) { throw new Win32Exception(); } var credentials = new IntPtr[count]; // convert pointer to array to array of pointers for (var n = 0; n < count; n++) { credentials[n] = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr))); } var login = credentials // convert each pointer to a structure .Select(ptr => (WinCred.Credential)Marshal.PtrToStructure(ptr, typeof(WinCred.Credential))) // select the structure with the matching credential name .FirstOrDefault(cred => cred.targetName.StartsWith(_credential)); // get the password var currentPass = Marshal.PtrToStringUni(login.credentialBlob, (int)login.credentialBlobSize / 2); var lastError = Marshal.GetLastWin32Error(); if (lastError != 0) { throw new Win32Exception(lastError); } // finally try to log in var hostArr = login.targetName.Substring(_credential.Length + 1).Split(':'); var host = hostArr[0]; var port = hostArr.Length > 1 ? int.Parse(hostArr[1]) : 143; var @params = new ImapParams { Host = host, Username = login.userName, Password = currentPass, Port = port, Validate = false, Ssl = port == 993 || port == 995, Queue = queue }; // start a thread to check for e-mail ThreadPool.QueueUserWorkItem(MailPoller, @params); }
private static bool GetLogin(string startsWith, out string username, out string password) { try { // get the login information out of the credential store int count; IntPtr pCredentials; var ret = AdvApi32.CredEnumerate(null, 0, out count, out pCredentials); if (!ret) { throw new Win32Exception(); } var credentials = new IntPtr[count]; // convert pointer to array to array of pointers for (var n = 0; n < count; n++) { credentials[n] = Marshal.ReadIntPtr(pCredentials, n * Marshal.SizeOf(typeof(IntPtr))); } var login = credentials // convert each pointer to a structure .Select(ptr => (WinCred.Credential)Marshal.PtrToStructure(ptr, typeof(WinCred.Credential))) // select the structure with the matching credential name .FirstOrDefault(cred => cred.targetName.StartsWith(startsWith)); // get the password var currentPass = Marshal.PtrToStringUni(login.credentialBlob, (int)login.credentialBlobSize / 2); var lastError = Marshal.GetLastWin32Error(); if (lastError != 0) { throw new Win32Exception(lastError); } username = login.userName; password = currentPass; return(true); } catch (Exception ex) { Log.Error("There was an error finding the credentials for Facebook.", ex); username = null; password = null; return(false); } }