public void Store(string resource, PasswordCredential credential) { // Validations. byte[] byteArray = Encoding.Unicode.GetBytes(credential.Password); // Go ahead with what we have are stuff it into the CredMan structures. Credential cred = new Credential { TargetName = resource, UserName = credential.UserName, CredentialBlob = credential.Password, CredentialBlobSize = (uint)byteArray.Length, AttributeCount = 0, Attributes = IntPtr.Zero, Comment = null, TargetAlias = null, Type = CRED_TYPE.GENERIC, Persist = CRED_PERSIST.LOCAL_MACHINE }; NativeCredential ncred = NativeCredential.GetNativeCredential(cred); // Write the info into the CredMan storage. bool written = CredWrite(ref ncred, 0); int lastError = Marshal.GetLastWin32Error(); if (!written) { string message = "CredWrite failed with the error code " + lastError.ToString(); throw new InvalidOperationException(message); } }
/// <summary> /// 向添加计算机的凭据管理其中添加凭据 /// </summary> /// <param name="key">internet地址或者网络地址</param> /// <param name="userName">用户名</param> /// <param name="secret">密码</param> /// <param name="type">密码类型</param> /// <param name="credPersist"></param> /// <returns></returns> public static int WriteCred(string key, string userName, string secret, CRED_TYPE type, CRED_PERSIST credPersist) { byte[] byteArray = Encoding.Unicode.GetBytes(secret); if (byteArray.Length > 512) { throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes."); } Credential cred = new Credential(); cred.TargetName = key; cred.CredentialBlob = secret; cred.CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length; cred.AttributeCount = 0; cred.Attributes = IntPtr.Zero; cred.UserName = userName; cred.Comment = null; cred.TargetAlias = null; cred.Type = type; cred.Persist = credPersist; #region // var cred = new Credential // { // TargetName = key, // CredentialBlob = secret, // CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length, // AttributeCount = 0, // Attributes = IntPtr.Zero, // UserName = userName, // Comment = null, // TargetAlias = null, // Type = type, // Persist = credPersist // }; #endregion NativeCredential ncred = NativeCredential.GetNativeCredential(cred); bool written = CredWrite(ref ncred, 0); int lastError = Marshal.GetLastWin32Error(); if (written) { return(0); } string message = ""; if (lastError == 1312) { message = (string.Format(String.Format("Failed to save {0} with error code {{0}}.", key), lastError) + " This error typically occurrs on home editions of Windows XP and Vista. Verify the version of Windows is Pro/Business or higher."); } else { message = string.Format(String.Format("Failed to save {0} with error code {{0}}.", key), lastError); } return(1); }
/// <summary> /// 向添加计算机的凭据管理其中添加凭据 /// </summary> /// <param name="key">internet地址或者网络地址</param> /// <param name="userName">用户名</param> /// <param name="secret">密码</param> /// <param name="type">密码类型</param> /// <param name="credPersist"></param> /// <returns></returns> public static int WriteCred(string key, string userName, string secret, CRED_TYPE type, CRED_PERSIST credPersist) { var byteArray = Encoding.Unicode.GetBytes(secret); if (byteArray.Length > 512) { throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes."); } var cred = new Credential { TargetName = key, CredentialBlob = secret, CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length, AttributeCount = 0, Attributes = IntPtr.Zero, UserName = userName, Comment = null, TargetAlias = null, Type = type, Persist = credPersist }; var ncred = NativeCredential.GetNativeCredential(cred); var written = CredWrite(ref ncred, 0); var lastError = Marshal.GetLastWin32Error(); if (written) { return(0); } var message = ""; if (lastError == 1312) { message = (string.Format("Failed to save " + key + " with error code {0}.", lastError) + " This error typically occurrs on home editions of Windows XP and Vista. Verify the version of Windows is Pro/Business or higher."); } else { message = string.Format("Failed to save " + key + " with error code {0}.", lastError); } MessageBox.Show(message); return(1); }
public static int WriteCred(string key, string username, string secret) { // Validations. byte[] byteArray = Encoding.Unicode.GetBytes(secret); if (byteArray.Length > 512) { throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes."); } // Go ahead with what we have are stuff it into the CredMan structures. Credential cred = new Credential(); cred.TargetName = key; cred.UserName = username; cred.CredentialBlob = secret; cred.CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(secret).Length; cred.AttributeCount = 0; cred.Attributes = IntPtr.Zero; cred.Comment = null; cred.TargetAlias = null; cred.Type = CRED_TYPE.GENERIC; cred.Persist = Persistence.CRED_PERSIST_LOCAL_MACHINE; NativeCredential ncred = NativeCredential.GetNativeCredential(cred); // Write the info into the CredMan storage. bool written = CredWrite(ref ncred, 0); int lastError = Marshal.GetLastWin32Error(); if (written) { return(0); } else { return(lastError); } }