ICredentials Interface
.NET Framework 3.0
Provides the base authentication interface for retrieving credentials for Web client authentication.
Namespace: System.Net
Assembly: System (in system.dll)
Assembly: System (in system.dll)
The ICredentials interface provides the GetCredential method to objects that supply network credentials to applications.
The following example illustrates how to use the ICredentials interface.
Class CredentialInfo
Public uriObj As Uri
Public authenticationType As [String]
Public networkCredentialObj As NetworkCredential
Public Sub New(uriObj As Uri, authenticationType As [String], networkCredentialObj As NetworkCredential)
Me.uriObj = uriObj
Me.authenticationType = authenticationType
Me.networkCredentialObj = networkCredentialObj
End Sub 'New
End Class 'CredentialInfo
Private arrayListObj As ArrayList
Public Sub New()
arrayListObj = New ArrayList()
End Sub 'New
Public Sub Add(uriObj As Uri, authenticationType As [String], credential As NetworkCredential)
' adds a 'CredentialInfo' object into a list
arrayListObj.Add(New CredentialInfo(uriObj, authenticationType, credential))
End Sub 'Add
' Remove the 'CredentialInfo' object from the list which matches to the given 'Uri' and 'AuthenticationType'
Public Sub Remove(uriObj As Uri, authenticationType As [String])
Dim index As Integer
For index = 0 To arrayListObj.Count - 1
Dim credentialInfo As CredentialInfo = CType(arrayListObj(index), CredentialInfo)
If uriObj.Equals(credentialInfo.uriObj) And authenticationType.Equals(credentialInfo.authenticationType) Then
arrayListObj.RemoveAt(index)
End If
Next index
End Sub 'Remove
Public Function GetCredential(uriObj As Uri, authenticationType As [String]) As NetworkCredential Implements ICredentials.GetCredential
Dim index As Integer
For index = 0 To arrayListObj.Count - 1
Dim credentialInfoObj As CredentialInfo = CType(arrayListObj(index), CredentialInfo)
If uriObj.Equals(credentialInfoObj.uriObj) And authenticationType.Equals(credentialInfoObj.authenticationType) Then
Return credentialInfoObj.networkCredentialObj
End If
Next index
Return Nothing
End Function 'GetCredential
class CredentialList implements ICredentials
{
class CredentialInfo
{
public Uri uriObj;
public String authenticationType;
public NetworkCredential networkCredentialObj;
public CredentialInfo(Uri uriObj, String authenticationType,
NetworkCredential networkCredentialObj)
{
this.uriObj = uriObj;
this.authenticationType = authenticationType;
this.networkCredentialObj = networkCredentialObj;
} //CredentialInfo
} //CredentialInfo
private ArrayList arrayListObj;
public CredentialList()
{
arrayListObj = new ArrayList();
} //CredentialList
public void Add(Uri uriObj, String authenticationType,
NetworkCredential credential)
{
// Add a 'CredentialInfo' object into a list.
arrayListObj.Add(new CredentialInfo(uriObj,
authenticationType, credential));
} //Add
// Remove the 'CredentialInfo' object from the list that matches to
// the given 'Uri' and 'AuthenticationType'
public void Remove(Uri uriObj, String authenticationType)
{
for (int index = 0; index < arrayListObj.get_Count(); index++) {
CredentialInfo credentialInfo = (CredentialInfo)
arrayListObj.get_Item(index);
if (uriObj.Equals(credentialInfo.uriObj) &&
authenticationType.Equals(credentialInfo.authenticationType)) {
arrayListObj.RemoveAt(index);
}
}
} //Remove
public NetworkCredential GetCredential(Uri uriObj,
String authenticationType)
{
for (int index = 0; index < arrayListObj.get_Count(); index++) {
CredentialInfo credentialInfoObj = (CredentialInfo)
arrayListObj.get_Item(index);
if (uriObj.Equals(credentialInfoObj.uriObj) &&
authenticationType.Equals(credentialInfoObj.
authenticationType)) {
return credentialInfoObj.networkCredentialObj;
}
}
return null;
} //GetCredential
} //CredentialList
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: