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 FromSecAuthenticationType(SecAuthenticationType type) { switch (type){ case SecAuthenticationType.Ntlm: return NTLM; case SecAuthenticationType.Msn: return MSN; case SecAuthenticationType.Dpa: return DPA; case SecAuthenticationType.Rpa: return RPA; case SecAuthenticationType.HttpBasic: return HTTPBasic; case SecAuthenticationType.HttpDigest: return HTTPDigest; case SecAuthenticationType.HtmlForm: return HTMLForm; case SecAuthenticationType.Default: return Default; default: throw new ArgumentException ("type"); } }