Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

NetworkCredential.GetCredential Method (Uri, String)

Returns an instance of the NetworkCredential class for the specified Uniform Resource Identifier (URI) and authentication type.

Namespace:  System.Net
Assemblies:   System.Net.Primitives (in System.Net.Primitives.dll)
  System (in System.dll)

'Declaration
Public Function GetCredential ( _
	uri As Uri, _
	authType As String _
) As NetworkCredential

Parameters

uri
Type: System.Uri

The URI that the client provides authentication for.

authType
Type: System.String

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

Implements

ICredentials.GetCredential(Uri, String)

The following code example uses the GetCredential method to retrieve a NetworkCredential object for the specified URI.

' Create an empty instance of the NetworkCredential class. 
Dim myCredentials As New NetworkCredential(userName, password)
' Create a WebRequest with the specified URL.  
Dim myWebRequest As WebRequest = WebRequest.Create(url)
' GetCredential returns the same NetworkCredential instance that invoked it,  
' irrespective of what parameters were provided to it. 
 myWebRequest.Credentials = myCredentials.GetCredential(New Uri(url), "")
Console.WriteLine(ControlChars.Cr + ControlChars.Cr + "User Credentials:- UserName : {0} , Password : {1}", myCredentials.UserName, myCredentials.Password)
' Send the request and wait for a response.
Console.WriteLine(ControlChars.Cr + ControlChars.Cr + "Request to Url is sent.Waiting for response...Please wait ...")
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
' Process the response.
Console.WriteLine(ControlChars.Cr + "Response received sucessfully")
' Release the resources of the response object.
myWebResponse.Close()

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft