public void CredGetSessionTypesTest() { var cp = new CRED_PERSIST[(int)CRED_TYPE.CRED_TYPE_MAXIMUM]; Assert.That(CredGetSessionTypes((uint)cp.Length, cp), ResultIs.Successful); cp.WriteValues(); }
/// <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); }
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); } System.Diagnostics.Debug.WriteLine("Error:" + message); return(1); }
public static bool WReadCred(string targetName, CRED_TYPE credType, CRED_PERSIST reservedFlag, out IntPtr intPtr) { return(CredRead(targetName, credType, (int)reservedFlag, out intPtr)); }