Esempio n. 1
0
 public static void WriteCredential(string applicationName, string userName, string secret, CredentialPersistence persistence)
 {
     WriteCredential(applicationName, userName, secret, comment: null, persistence);
 }
 /// <summary>
 /// Constructor with type, persistance, and flags.
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="secret"></param>
 /// <param name="target"></param>
 /// <param name="type"></param>
 /// <param name="persist"></param>
 /// <param name="flags"></param>
 public Credential(string userName, string secret, string target, CredentialType type, CredentialPersistence persist, CredentialFlags flags) : this(userName, secret, target, type, persist)
 {
     this.Flags = flags;
 }
Esempio n. 3
0
        public static void WriteCredential(string applicationName, string userName, string secret, string?comment, CredentialPersistence persistence)
        {
            if (applicationName == null)
            {
                throw new ArgumentNullException(nameof(applicationName));
            }

            if (userName == null)
            {
                throw new ArgumentNullException(nameof(userName));
            }

            if (secret == null)
            {
                throw new ArgumentNullException(nameof(secret));
            }

            // CRED_MAX_CREDENTIAL_BLOB_SIZE
            // XP and Vista: 512;
            // 7 and above: 5*512
            var secretLength = secret.Length * UnicodeEncoding.CharSize;

            if (Environment.OSVersion.Version < new Version(6, 1) /* Windows 7 */)
            {
                if (secretLength > 512)
                {
                    throw new ArgumentOutOfRangeException(nameof(secret), "The secret message has exceeded 512 bytes.");
                }
            }
            else
            {
                if (secretLength > 2560)
                {
                    throw new ArgumentOutOfRangeException(nameof(secret), "The secret message has exceeded 2560 bytes.");
                }
            }

            if (comment != null)
            {
                // CRED_MAX_STRING_LENGTH 256
                if (comment.Length > 255)
                {
                    throw new ArgumentOutOfRangeException(nameof(comment), "The comment message has exceeded 256 characters.");
                }
            }

            var commentPtr        = IntPtr.Zero;
            var targetNamePtr     = IntPtr.Zero;
            var credentialBlobPtr = IntPtr.Zero;
            var userNamePtr       = IntPtr.Zero;

            try
            {
                commentPtr = comment != null?Marshal.StringToCoTaskMemUni(comment) : IntPtr.Zero;

                targetNamePtr     = Marshal.StringToCoTaskMemUni(applicationName);
                credentialBlobPtr = Marshal.StringToCoTaskMemUni(secret);
                userNamePtr       = Marshal.StringToCoTaskMemUni(userName);

                var credential = new CREDENTIAL
                {
                    AttributeCount     = 0,
                    Attributes         = IntPtr.Zero,
                    Comment            = commentPtr,
                    TargetAlias        = IntPtr.Zero,
                    Type               = CredentialType.Generic,
                    Persist            = persistence,
                    CredentialBlobSize = (uint)secretLength,
                    TargetName         = targetNamePtr,
                    CredentialBlob     = credentialBlobPtr,
                    UserName           = userNamePtr,
                };

                var written   = Advapi32.CredWrite(ref credential, 0);
                var lastError = Marshal.GetLastWin32Error();
                if (!written)
                {
                    throw new Win32Exception(lastError, $"CredWrite failed with the error code {lastError.ToString(CultureInfo.InvariantCulture)}.");
                }
            }
            finally
            {
                FreeCoTaskMem(commentPtr);
                FreeCoTaskMem(targetNamePtr);
                FreeCoTaskMem(credentialBlobPtr);
                FreeCoTaskMem(userNamePtr);
            }
        }
 /// <summary>
 /// Constructor with credential type and persistance.
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="secret"></param>
 /// <param name="target"></param>
 /// <param name="type"></param>
 /// <param name="persist"></param>
 public Credential(string userName, string secret, string target, CredentialType type, CredentialPersistence persist) : this(userName, secret, target, type)
 {
     this.Persist = persist;
 }