public bool Save() { CheckNotDisposed(); _unmanagedCodePermission.Demand(); byte[] passwordBytes = Encoding.Unicode.GetBytes(Password); if (Password.Length > (512)) { throw new ArgumentOutOfRangeException("The password has exceeded 512 bytes."); } var credential = new NativeMethods.CREDENTIAL { TargetName = Target, UserName = Username, CredentialBlob = Marshal.StringToCoTaskMemUni(Password), CredentialBlobSize = passwordBytes.Length, Comment = Description, Type = (int)Type, Persist = (int)PersistanceType }; var result = NativeMethods.CredWrite(ref credential, 0); if (!result) { return(false); } LastWriteTimeUtc = DateTime.UtcNow; return(true); }
internal void LoadInternal(NativeMethods.CREDENTIAL credential) { Username = credential.UserName; if (credential.CredentialBlobSize > 0) { Password = Marshal.PtrToStringUni(credential.CredentialBlob, credential.CredentialBlobSize / 2); } Target = credential.TargetName; Type = (CredentialType)credential.Type; PersistanceType = (PersistanceType)credential.Persist; Description = credential.Comment; LastWriteTimeUtc = DateTime.FromFileTimeUtc(credential.LastWritten); }