ICredentials.GetCredential Method
.NET Framework 3.0
Returns a NetworkCredential object that is associated with the specified URI, and authentication type.
Namespace: System.Net
Assembly: System (in system.dll)
Assembly: System (in system.dll)
NetworkCredential GetCredential ( Uri uri, String authType )
function GetCredential (
uri : Uri,
authType : String
) : NetworkCredential
Not applicable.
Parameters
- uri
The Uri that the client is providing authentication for.
- authType
The type of authentication, as defined in the IAuthenticationModule.AuthenticationType property.
Return Value
The NetworkCredential that is associated with the specified URI and authentication type, or, if no credentials are available, a null reference (Nothing in Visual Basic).The following uses GetCredential to retrieve a NetworkCredential instance.
class CredentialList : 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; } } private ArrayList arrayListObj; public CredentialList() { arrayListObj = new ArrayList(); } public void Add (Uri uriObj, String authenticationType, NetworkCredential credential) { // Add a 'CredentialInfo' object into a list. arrayListObj.Add (new CredentialInfo(uriObj, authenticationType, credential)); } // 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.Count; index++) { CredentialInfo credentialInfo = (CredentialInfo)arrayListObj[index]; if(uriObj.Equals(credentialInfo.uriObj)&& authenticationType.Equals(credentialInfo.authenticationType)) arrayListObj.RemoveAt(index); } } public NetworkCredential GetCredential (Uri uriObj, String authenticationType) { for(int index=0;index < arrayListObj.Count; index++) { CredentialInfo credentialInfoObj = (CredentialInfo)arrayListObj[index]; if(uriObj.Equals(credentialInfoObj.uriObj) && authenticationType.Equals(credentialInfoObj.authenticationType)) return credentialInfoObj.networkCredentialObj; } return null; } };
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.