public void CredReadTest() { const CRED_TYPE ct = CRED_TYPE.CRED_TYPE_GENERIC; var genCred = CredEnumerate().FirstOrDefault(f => f.Type == ct).TargetName; Assert.That(CredRead(genCred, ct, 0, out var cred), Is.True); }
/// <summary> /// Read the user name and password from the Windows Credential Manager /// </summary> /// <param name="targetName">name of the credential to read</param> /// <param name="credType">Type of the credential to read</param> /// <param name="flags">Currently reserved and must be zero</param> /// <param name="userName">Stored user name</param> /// <param name="password">Store password</param> /// <returns></returns> public static bool ReadCredentials(string targetName, CRED_TYPE credType, uint flags, out string userName, out SecureString password) { userName = null; password = null; var pCredentials = IntPtr.Zero; // See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374804(v=vs.85).aspx var result = CredRead(targetName, credType, flags, out pCredentials); if (result == true) { var cred = (Credential)Marshal.PtrToStructure(pCredentials, typeof(Credential)); userName = cred.UserName; password = new SecureString(); foreach (var passChar in cred.CredentialBlob) { password.AppendChar(passChar); } password.MakeReadOnly(); // See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374796(v=vs.85).aspx CredFree(pCredentials); } return(result); }
public static int CredDelete(string target, CRED_TYPE type) { if (!CredDeleteW(target, type, 0)) { return(Marshal.GetHRForLastWin32Error()); } return(0); }
/// <summary> /// 資格情報の削除 /// </summary> /// <param name="targetName"></param> /// <param name="type"></param> /// <param name="flags"></param> private static void Delete(string targetName, CRED_TYPE type, CRED_FLAGS flags) { if (!CredDelete(targetName, type, flags)) { Log.Instance.LogWrite(LogState.ERROR, GetErrorMessage()); //throw new ApplicationException("資格情報の削除に失敗しました。"); } }
/// <summary> /// 資格情報の削除 /// </summary> /// <param name="targetName"></param> /// <param name="type"></param> /// <param name="flags"></param> private static void Delete(string targetName, CRED_TYPE type, CRED_FLAGS flags) { if (!CredDelete(targetName, type, flags)) { Console.WriteLine(GetErrorMessage()); throw new ApplicationException("資格情報の削除に失敗しました。"); } }
/// <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="targetName"></param> /// <param name="type"></param> /// <returns></returns> public static ManagedCredential Read(string targetName, CRED_TYPE type) { var credential = IntPtr.Zero; if (!CredRead(targetName, type, 0, out credential)) { Log.Instance.LogWrite(LogState.ERROR, GetErrorMessage()); //throw new ApplicationException("資格情報の取得に失敗しました。"); } return(ConvertToManagedCredential(credential)); }
/// <summary> /// 資格情報の読み込み /// </summary> /// <param name="targetName"></param> /// <param name="type"></param> /// <returns></returns> public static ManagedCredential Read(string targetName, CRED_TYPE type) { var credential = IntPtr.Zero; if (!CredRead(targetName, type, 0, out credential)) { Console.WriteLine(GetErrorMessage()); throw new ApplicationException("資格情報の取得に失敗しました。"); } return(ConvertToManagedCredential(credential)); }
public static int CredRead(string target, CRED_TYPE type, out Credential Credential) { IntPtr pCredential = IntPtr.Zero; Credential = new Credential(); if (!CredReadW(target, type, 0, out pCredential)) { return(Marshal.GetHRForLastWin32Error()); } CriticalCredentialHandle CredHandle = new CriticalCredentialHandle(pCredential); Credential = CredHandle.GetCredential(); return(0); }
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 DeleteCredentials(string target, CRED_TYPE type = CRED_TYPE.GENERIC) { return(CredDelete(target, type, 0)); }
static extern bool CredDelete(string target, CRED_TYPE type, [In] UInt32 flags);
public static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
internal static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(CredentialMarshaler))] out Credential credential);
public static extern Boolean CredReadW(String target, CRED_TYPE type, Int32 reservedFlag, out IntPtr credentialPtr);
private static extern bool CredRead([In] string targetName, [In] CRED_TYPE type, [In] int reservedFlag, [Out] out IntPtr credentialPtr);
public static IntPtr CredRead(string target, CRED_TYPE type) { ThrowIfFailed(Imports.CredRead(target, type, 0, out IntPtr userCredential)); return(userCredential); }
internal static extern bool CredDelete(string target, CRED_TYPE type, int reservedFlag);
private static extern bool CredRead(string targetName, CRED_TYPE type, CRED_FLAGS flags, out IntPtr pCredential);
private static extern bool CredDeleteW([In] string target, [In] CRED_TYPE type, [In] int reservedFlag);
private static extern bool CredReadW([In] string target, [In] CRED_TYPE type, [In] int reservedFlag, out IntPtr CredentialPtr);
public static (bool success, string username, string password) GetCredentials(string target, CRED_TYPE credType = CRED_TYPE.GENERIC) { var username = string.Empty; var password = string.Empty; var credentialPtr = IntPtr.Zero; try { bool success = Advapi32dll.CredRead(target, Advapi32dll.CRED_TYPE.GENERIC, 0, out credentialPtr); if (success) { var credential = (Advapi32dll.CREDENTIAL)Marshal.PtrToStructure(credentialPtr, typeof(Advapi32dll.CREDENTIAL)); username = credential.UserName; if (credential.CredentialBlobSize > 2) { password = Marshal.PtrToStringUni(credential.CredentialBlob, (int)credential.CredentialBlobSize / 2); } } return(success, username, password); } finally { if (credentialPtr != IntPtr.Zero) { CredFree(credentialPtr); } } }
public static (bool success, int errorCode) StoreCredentials(string target, string username, string password, CRED_TYPE type = CRED_TYPE.GENERIC) { byte[] byteArray = Encoding.Unicode.GetBytes(password); if (byteArray.Length > 512) { throw new ArgumentOutOfRangeException("The secret message has exceeded 512 bytes."); } CREDENTIAL credentailPtr = new CREDENTIAL { AttributeCount = 0, CredAttribute = IntPtr.Zero, Comment = null, TargetAlias = null, Type = type, Persist = (UInt32)CRED_PERSIST.ENTERPRISE, CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(password).Length, TargetName = target, CredentialBlob = Marshal.StringToCoTaskMemUni(password), UserName = username }; bool written = CredWrite(ref credentailPtr, 0); int lastError = Marshal.GetLastWin32Error(); if (written) { return(true, 0); } return(false, lastError); }
/// <summary> /// 读取凭据 /// </summary> /// <param name="targetName"></param> /// <param name="credType"></param> /// <param name="reservedFlag"></param> /// <param name="intPtr"></param> /// <returns></returns> public static bool WReadCred(string targetName, CRED_TYPE credType, int reservedFlag, out IntPtr intPtr) { return(CredRead(targetName, CRED_TYPE.DOMAIN_PASSWORD, reservedFlag, out intPtr)); }
public static void CredDelete(string target, CRED_TYPE type) => ThrowIfFailed(Imports.CredDelete(target, type, 0));
//读取凭据信息 static extern bool CredRead(string target, CRED_TYPE type, int reservedFlag, out IntPtr CredentialPtr);
private static extern bool CredDelete(string targetName, CRED_TYPE type, UInt32 flags);
public static extern bool CredRead( string target, CRED_TYPE type, int reservedFlag, out CredentialData userCredential);
public static bool WReadCred(string targetName, CRED_TYPE credType, CRED_PERSIST reservedFlag, out IntPtr intPtr) { return(CredRead(targetName, credType, (int)reservedFlag, out intPtr)); }
private static extern bool CredDelete(string targetName, CRED_TYPE type, CRED_FLAGS flags);
/// <summary> /// 删除凭据 /// </summary> /// <param name="target"></param> /// <param name="type"></param> /// <param name="flags"></param> /// <returns></returns> public static bool DeleteCred(string target, CRED_TYPE type, int flags) { return(CredDelete(target, type, flags)); }
/// <summary> /// 資格情報の読み込み /// </summary> /// <param name="targetName"></param> /// <param name="type"></param> /// <returns></returns> public static ManagedCredential Read(string targetName, CRED_TYPE type) { var credential = IntPtr.Zero; if (!CredRead(targetName, type, 0, out credential)) { Console.WriteLine(GetErrorMessage()); throw new ApplicationException("資格情報の取得に失敗しました。"); } return ConvertToManagedCredential(credential); }
//删除凭据 static extern bool CredDelete(string target, CRED_TYPE type, int flags);
public static extern bool CredDelete(string target, CRED_TYPE type, int flags);