using System; using System.Collections; using System.Collections.Generic; using System.Linq; using static Vanara.PInvoke.BITS; namespace Vanara.IO { /// Represents a single BITS job credential. /// public class BackgroundCopyJobCredential : IComparable { /// Initializes a new instance of the class. /// The scheme. /// The target. /// The user name. /// The password. public BackgroundCopyJobCredential(BackgroundCopyJobCredentialScheme scheme, BackgroundCopyJobCredentialTarget target, string username, string password) { Scheme = scheme; Target = target; UserName = username; Password = password; } /// Gets the password. /// The password. public string Password { get; } /// Gets the scheme. /// The scheme. public BackgroundCopyJobCredentialScheme Scheme { get; } /// Gets the target. /// The target. public BackgroundCopyJobCredentialTarget Target { get; } /// Gets the name of the user. /// The name of the user. public string UserName { get; } internal uint Key => BackgroundCopyJobCredentials.MakeKey(Scheme, Target); int IComparable.CompareTo(BackgroundCopyJobCredential other) => Comparer.Default.Compare(Key, other.Key); internal BG_AUTH_CREDENTIALS GetNative() { var cr = new BG_AUTH_CREDENTIALS { Scheme = (BG_AUTH_SCHEME)Scheme, Target = (BG_AUTH_TARGET)Target }; cr.Credentials.Basic.UserName = UserName; cr.Credentials.Basic.Password = Password; return cr; } } /// The list of credentials for a job. /// /// public class BackgroundCopyJobCredentials : IDisposable, ICollection { private Dictionary dict; private IBackgroundCopyJob2 ijob2; internal BackgroundCopyJobCredentials(IBackgroundCopyJob2 job) { ijob2 = job; } /// Gets the number of elements contained in the . public int Count => dict?.Count ?? 0; /// Gets a value indicating whether the is read-only. bool ICollection.IsReadOnly => false; private Dictionary Values => dict ??= new Dictionary(); /// Gets the with the specified scheme and target. /// The credential scheme. /// The credential target. /// The . public BackgroundCopyJobCredential this[BackgroundCopyJobCredentialScheme scheme, BackgroundCopyJobCredentialTarget target] => Values[MakeKey(scheme, target)]; /// Adds the specified credential. /// The credential. public void Add(BackgroundCopyJobCredential cred) { var cr = cred.GetNative(); ijob2.SetCredentials(ref cr); Values.Add(cred.Key, cred); } /// Adds the specified credential. /// The credential scheme. /// The credential target. /// The username. /// The password. public void Add(BackgroundCopyJobCredentialScheme scheme, BackgroundCopyJobCredentialTarget target, string username, string password) { Add(new BackgroundCopyJobCredential(scheme, target, username, password)); } /// Removes all items from the . public void Clear() { if (dict is null) return; foreach (var key in Values.Keys) Remove(Values[key]); } /// Determines whether the contains a specific value. /// The object to locate in the . /// true if is found in the ; otherwise, false. public bool Contains(BackgroundCopyJobCredential item) => dict is not null && Values.ContainsKey(item.Key); /// /// Copies the elements of the to an , starting at a particular index. /// /// /// The one-dimensional that is the destination of the elements copied from . The /// must have zero-based indexing. /// /// The zero-based index in at which copying begins. public void CopyTo(BackgroundCopyJobCredential[] array, int arrayIndex) { if (dict is null) return; Array.Copy(Values.Values.ToArray(), 0, array, arrayIndex, Count); } /// Returns an enumerator that iterates through the collection. /// A that can be used to iterate through the collection. public IEnumerator GetEnumerator() => Values.Values.GetEnumerator(); /// Removes the specified scheme. /// The scheme. /// The target. /// public bool Remove(BackgroundCopyJobCredentialScheme scheme, BackgroundCopyJobCredentialTarget target) { try { ijob2.RemoveCredentials((BG_AUTH_TARGET)target, (BG_AUTH_SCHEME)scheme); if (dict is not null) Values.Remove(MakeKey(scheme, target)); return true; } catch { return false; } } /// Removes the first occurrence of a specific object from the . /// The object to remove from the . /// /// true if was successfully removed from the ; otherwise, false. This method also returns false if /// is not found in the original . /// public bool Remove(BackgroundCopyJobCredential item) => Remove(item.Scheme, item.Target); /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. void IDisposable.Dispose() { ijob2 = null; } /// Returns an enumerator that iterates through a collection. /// An object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); internal static uint MakeKey(BackgroundCopyJobCredentialScheme scheme, BackgroundCopyJobCredentialTarget target) => PInvoke.Macros.MAKELONG((ushort)scheme, (ushort)target); } }