public static SecStatusCode FindInternetPassword( string serverName, string accountName, out byte[] password, SecProtocol protocolType = SecProtocol.Http, short port = 0, string path = null, SecAuthenticationType authenticationType = SecAuthenticationType.Default, string securityDomain = null) { password = null; GCHandle serverHandle = new GCHandle (); GCHandle securityDomainHandle = new GCHandle (); GCHandle accountHandle = new GCHandle (); GCHandle pathHandle = new GCHandle (); int serverNameLength = 0; IntPtr serverNamePtr = IntPtr.Zero; int securityDomainLength = 0; IntPtr securityDomainPtr = IntPtr.Zero; int accountNameLength = 0; IntPtr accountNamePtr = IntPtr.Zero; int pathLength = 0; IntPtr pathPtr = IntPtr.Zero; IntPtr passwordPtr = IntPtr.Zero; try { if (!String.IsNullOrEmpty(serverName)) { var bytes = System.Text.Encoding.UTF8.GetBytes (serverName); serverNameLength = bytes.Length; serverHandle = GCHandle.Alloc (bytes, GCHandleType.Pinned); serverNamePtr = serverHandle.AddrOfPinnedObject (); } if (!String.IsNullOrEmpty(securityDomain)) { var bytes = System.Text.Encoding.UTF8.GetBytes (securityDomain); securityDomainLength = bytes.Length; securityDomainHandle = GCHandle.Alloc (bytes, GCHandleType.Pinned); } if (!String.IsNullOrEmpty(accountName)) { var bytes = System.Text.Encoding.UTF8.GetBytes (accountName); accountNameLength = bytes.Length; accountHandle = GCHandle.Alloc (bytes, GCHandleType.Pinned); accountNamePtr = accountHandle.AddrOfPinnedObject (); } if (!String.IsNullOrEmpty(path)) { var bytes = System.Text.Encoding.UTF8.GetBytes (path); pathLength = bytes.Length; pathHandle = GCHandle.Alloc (bytes, GCHandleType.Pinned); pathPtr = pathHandle.AddrOfPinnedObject (); } int passwordLength = 0; SecStatusCode code = SecKeychainFindInternetPassword( IntPtr.Zero, serverNameLength, serverNamePtr, securityDomainLength, securityDomainPtr, accountNameLength, accountNamePtr, pathLength, pathPtr, port, SecProtocolKeys.FromSecProtocol(protocolType), KeysAuthenticationType.FromSecAuthenticationType(authenticationType), out passwordLength, out passwordPtr, IntPtr.Zero); if (code == SecStatusCode.Success && passwordLength > 0) { password = new byte[passwordLength]; Marshal.Copy(passwordPtr, password, 0, passwordLength); } return code; } finally { if (serverHandle.IsAllocated) serverHandle.Free(); if (accountHandle.IsAllocated) accountHandle.Free(); if (securityDomainHandle.IsAllocated) securityDomainHandle.Free(); if (pathHandle.IsAllocated) pathHandle.Free(); if (passwordPtr != IntPtr.Zero) SecKeychainItemFreeContent(IntPtr.Zero, passwordPtr); } }
public static IntPtr FromSecProtocol(SecProtocol protocol) { switch (protocol){ case SecProtocol.Ftp: return AttrProtocolFTP; case SecProtocol.FtpAccount: return AttrProtocolFTPAccount; case SecProtocol.Http: return AttrProtocolHTTP; case SecProtocol.Irc: return AttrProtocolIRC; case SecProtocol.Nntp: return AttrProtocolNNTP; case SecProtocol.Pop3: return AttrProtocolPOP3; case SecProtocol.Smtp: return AttrProtocolSMTP; case SecProtocol.Socks:return AttrProtocolSOCKS; case SecProtocol.Imap:return AttrProtocolIMAP; case SecProtocol.Ldap:return AttrProtocolLDAP; case SecProtocol.AppleTalk:return AttrProtocolAppleTalk; case SecProtocol.Afp:return AttrProtocolAFP; case SecProtocol.Telnet:return AttrProtocolTelnet; case SecProtocol.Ssh:return AttrProtocolSSH; case SecProtocol.Ftps:return AttrProtocolFTPS; case SecProtocol.Https:return AttrProtocolHTTPS; case SecProtocol.HttpProxy:return AttrProtocolHTTPProxy; case SecProtocol.HttpsProxy:return AttrProtocolHTTPSProxy; case SecProtocol.FtpProxy:return AttrProtocolFTPProxy; case SecProtocol.Smb:return AttrProtocolSMB; case SecProtocol.Rtsp:return AttrProtocolRTSP; case SecProtocol.RtspProxy:return AttrProtocolRTSPProxy; case SecProtocol.Daap:return AttrProtocolDAAP; case SecProtocol.Eppc:return AttrProtocolEPPC; case SecProtocol.Ipp:return AttrProtocolIPP; case SecProtocol.Nntps:return AttrProtocolNNTPS; case SecProtocol.Ldaps:return AttrProtocolLDAPS; case SecProtocol.Telnets:return AttrProtocolTelnetS; case SecProtocol.Imaps:return AttrProtocolIMAPS; case SecProtocol.Ircs:return AttrProtocolIRCS; case SecProtocol.Pop3s: return AttrProtocolPOP3S; } throw new ArgumentException ("protocol"); }