NetworkCredentialCachedList Class

public sealed class NetworkCredentialCachedList : System.Collections.CollectionBase

Represents a list of cached network credentials.

Summary

The NetworkCredentialCachedList class provides a mechanism to manage a collection of NetworkCredential objects that have been cached by the system. This is particularly useful in scenarios where an application frequently requires network credentials and wants to avoid repeatedly prompting the user or re-fetching them.

This class inherits from System.Collections.CollectionBase, providing standard collection management functionalities like adding, removing, and accessing credentials by index.

Fields

This class does not declare any fields.

Constructors

NetworkCredentialCachedList()

public NetworkCredentialCachedList()

Initializes a new instance of the NetworkCredentialCachedList class with default settings.

Properties

Count

public override int Count { get; }

Gets the number of NetworkCredential objects contained in the NetworkCredentialCachedList.

Item

public NetworkCredential this[int index] { get; set; }

Gets or sets the NetworkCredential object at the specified index.

Parameters:

index: The zero-based index of the element to get or set.

Methods

Add

public int Add(NetworkCredential value)

Adds a NetworkCredential object to the end of the NetworkCredentialCachedList.

Parameters:

value: The NetworkCredential object to add.

Returns: The zero-based index at which the specified NetworkCredential object was added.

Clear

public void Clear()

Removes all NetworkCredential objects from the NetworkCredentialCachedList.

Contains

public bool Contains(NetworkCredential value)

Determines whether the NetworkCredentialCachedList contains the specified NetworkCredential object.

Parameters:

value: The NetworkCredential object to locate in the NetworkCredentialCachedList.

Returns: true if the specified NetworkCredential object is found in the list; otherwise, false.

CopyTo

public void CopyTo(NetworkCredential[] array, int index)

Copies the entire NetworkCredentialCachedList to a compatible one-dimensional NetworkCredential array, starting at the specified index of the target array.

Parameters:

array: The one-dimensional NetworkCredential array that is the destination of the elements copied from the NetworkCredentialCachedList. The array must have zero-based indexing.

index: The zero-based index in array at which to begin copying.

IndexOf

public int IndexOf(NetworkCredential value)

Searches for the specified NetworkCredential object and returns the zero-based index of the first occurrence within the entire NetworkCredentialCachedList.

Parameters:

value: The NetworkCredential object to search for.

Returns: The zero-based index of the first occurrence of value within the entire NetworkCredentialCachedList; otherwise, -1.

Remove

public void Remove(NetworkCredential value)

Removes the first occurrence of the specified NetworkCredential object from the NetworkCredentialCachedList.

Parameters:

value: The NetworkCredential object to remove from the NetworkCredentialCachedList.

Example Usage

using System;
using System.Net;
using System.Collections;

// ...

// Create a new cached credential list
NetworkCredentialCachedList cachedCredentials = new NetworkCredentialCachedList();

// Create some network credentials
NetworkCredential credential1 = new NetworkCredential("user1", "password123");
NetworkCredential credential2 = new NetworkCredential("admin", "securepass");

// Add credentials to the list
cachedCredentials.Add(credential1);
cachedCredentials.Add(credential2);

// Access a credential
Console.WriteLine($"First credential username: {cachedCredentials[0].UserName}");

// Check if a credential exists
if (cachedCredentials.Contains(credential1))
{
    Console.WriteLine("Credential1 is in the list.");
}

// Remove a credential
cachedCredentials.Remove(credential2);

// Clear the list
// cachedCredentials.Clear();

// Iterate through the list
foreach (NetworkCredential cred in cachedCredentials)
{
    Console.WriteLine($"Username: {cred.UserName}, Domain: {cred.Domain}");
}

Note

The NetworkCredentialCachedList class is primarily an internal component used by .NET Framework to manage credentials for certain network operations. Direct manipulation of this class might not be necessary in most application development scenarios.