ICredentials.GetCredential Method (Uri, String)


Returns a NetworkCredential object that is associated with the specified URI, and authentication type.

Namespace:   System.Net
Assembly:  System (in System.dll)

Function GetCredential (
	uri As Uri,
	authType As String
) As NetworkCredential


Type: System.Uri

The Uri that the client is providing authentication for.

Type: System.String

The type of authentication, as defined in the IAuthenticationModule.AuthenticationType property.

Return Value

Type: System.Net.NetworkCredential

The NetworkCredential that is associated with the specified URI and authentication type, or, if no credentials are available, null.

The GetCredential method returns a NetworkCredential instance that contains the credentials that are associated with the specified URI and authorization scheme. When no credentials are available, the GetCredential method returns null.

The following uses GetCredential to retrieve a NetworkCredential instance.

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 'NewNew
End Class 'CredentialInfo

Private arrayListObj As ArrayList

Public Sub New()
    arrayListObj = New ArrayList()
End Sub 'NewNew

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
        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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 3.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top