using System; using LibGit2Sharp.Core; using System.Security; using System.Runtime.InteropServices; namespace LibGit2Sharp { /// /// Class that uses to hold username and password credentials for remote repository access. /// public sealed class SecureUsernamePasswordCredentials : Credentials { /// /// Callback to acquire a credential object. /// /// The newly created credential object. /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired. protected internal override int GitCredentialHandler(out IntPtr cred) { if (Username == null || Password == null) { throw new InvalidOperationException("UsernamePasswordCredentials contains a null Username or Password."); } IntPtr passwordPtr = IntPtr.Zero; try { passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(Password); return NativeMethods.git_cred_userpass_plaintext_new(out cred, Username, Marshal.PtrToStringUni(passwordPtr)); } finally { Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr); } } /// /// Username for username/password authentication (as in HTTP basic auth). /// public string Username { get; set; } /// /// Password for username/password authentication (as in HTTP basic auth). /// public SecureString Password { get; set; } } }