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

Returns an instance of the NetworkCredential class for the specified URI and authentication type.

[Visual Basic]
Public Overridable Function GetCredential( _
   ByVal uri As Uri, _
   ByVal authType As String _
) As NetworkCredential Implements ICredentials.GetCredential
[C#]
public virtual NetworkCredential GetCredential(
 Uri uri,
 string authType
);
[C++]
public: virtual NetworkCredential* GetCredential(
 Uri* uri,
 String* authType
);
[JScript]
public function GetCredential(
   uri : Uri,
 authType : String
) : NetworkCredential;

Parameters

uri
The URI that the client is providing authentication for.
authType
The type of authentication requested, as defined in the IAuthenticationModule.AuthenticationType property.

Return Value

A NetworkCredential instance.

Implements

ICredentials.GetCredential

Example

[Visual Basic, C#, C++] The following example uses the GetCredential method to retrieve a NetworkCredential instance for the specified URI.

[Visual Basic] 
' 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()

[C#] 
// Create an empty instance of the NetworkCredential class.
NetworkCredential myCredentials = new NetworkCredential(userName,password);
// Create a webrequest with the specified URL. 
WebRequest myWebRequest = WebRequest.Create(url); 
myWebRequest.Credentials = myCredentials.GetCredential(new Uri(url),"");
Console.WriteLine("\n\nUser Credentials:- UserName : {0} , Password : {1}",myCredentials.UserName,myCredentials.Password);
// Send the request and wait for a response.
Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...Please wait ...");
WebResponse myWebResponse = myWebRequest.GetResponse();
// Process the response.
   Console.WriteLine("\nResponse received sucessfully");
// Release the resources of the response object.
myWebResponse.Close();

[C++] 
// Create an empty instance of the NetworkCredential class.
NetworkCredential* myCredentials =
   new NetworkCredential(userName, password);
// Create a webrequest with the specified URL.
WebRequest* myWebRequest = WebRequest::Create(url);
myWebRequest->Credentials =
   myCredentials->GetCredential(new Uri(url), S"");
Console::WriteLine(S"\n\nUser Credentials:- UserName : {0} , Password : {1}",
   myCredentials->UserName, myCredentials->Password);
// Send the request and wait for a response.
Console::WriteLine(S"\n\nRequest to Url is sent.Waiting for response...Please wait ...");
WebResponse* myWebResponse =
   myWebRequest->GetResponse();
// Process the response.
Console::WriteLine(S"\nResponse received sucessfully");
// Release the resources of the response object.
myWebResponse->Close();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

NetworkCredential Class | NetworkCredential Members | System.Net Namespace

Show:
© 2015 Microsoft