CredentialCache.GetCredential 메서드

정의

지정된 URI(Uniform Resource Identifier)나 호스트 및 인증 형식과 관련된 NetworkCredential 인스턴스를 반환합니다.

오버로드

GetCredential(Uri, String)

지정된 URI(Uniform Resource Identifier) 및 인증 형식과 관련된 NetworkCredential 인스턴스를 반환합니다.

GetCredential(String, Int32, String)

지정된 호스트, 포트 및 인증 프로토콜과 관련된 NetworkCredential 인스턴스를 반환합니다.

GetCredential(Uri, String)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

지정된 URI(Uniform Resource Identifier) 및 인증 형식과 관련된 NetworkCredential 인스턴스를 반환합니다.

public:
 virtual System::Net::NetworkCredential ^ GetCredential(Uri ^ uriPrefix, System::String ^ authType);
public System.Net.NetworkCredential GetCredential (Uri uriPrefix, string authType);
public System.Net.NetworkCredential? GetCredential (Uri uriPrefix, string authType);
abstract member GetCredential : Uri * string -> System.Net.NetworkCredential
override this.GetCredential : Uri * string -> System.Net.NetworkCredential
Public Function GetCredential (uriPrefix As Uri, authType As String) As NetworkCredential

매개 변수

uriPrefix
Uri

자격 증명이 권한을 부여하는 리소스의 URI 접두사를 지정하는 Uri입니다.

authType
String

uriPrefix에 명명된 리소스가 사용한 인증 체계입니다.

반환

NetworkCredential이거나 캐시에 일치하는 자격 증명이 없는 경우에는 null입니다.

구현

예외

uriPrefix 또는 authTypenull인 경우

예제

다음 코드 예제에서는 메서드를 GetCredential(Uri, String) 사용하여 지정된 URI 및 인증 유형과 연결된 instance 반환 NetworkCredential 합니다.

void Display( NetworkCredential^ credential )
{
   Console::WriteLine( "\nThe credentials are:" );
   Console::WriteLine( "\nUsername : {0} , Password : {1} , Domain : {2}", credential->UserName, credential->Password, credential->Domain );
}

void GetPage( String^ url, String^ userName, String^ password, String^ domainName )
{
   try
   {
      CredentialCache^ myCredentialCache = gcnew CredentialCache;

      // Dummy names used as credentials.
      myCredentialCache->Add( gcnew Uri( "http://microsoft.com/" ), "Basic", gcnew NetworkCredential( "user1","passwd1","domain1" ) );
      myCredentialCache->Add( gcnew Uri( "http://msdn.com/" ), "Basic", gcnew NetworkCredential( "user2","passwd2","domain2" ) );
      myCredentialCache->Add( gcnew Uri( url ), "Basic", gcnew NetworkCredential( userName,password,domainName ) );

      // Create a webrequest with the specified url.
      WebRequest^ myWebRequest = WebRequest::Create( url );

      // Call 'GetCredential' to obtain the credentials specific to our Uri.
      NetworkCredential^ myCredential = myCredentialCache->GetCredential( gcnew Uri( url ), "Basic" );
      Display( myCredential );

      // Associating only our credentials.
      myWebRequest->Credentials = myCredential;

      // Sends the request and waits for response.
      WebResponse^ myWebResponse = myWebRequest->GetResponse();

      // Process response here.
      Console::WriteLine( "\nResponse Received." );
      myWebResponse->Close();
   }
   catch ( WebException^ e ) 
   {
      if ( e->Response != nullptr )
            Console::WriteLine( "\r\nFailed to obtain a response. The following error occurred : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
      else
            Console::WriteLine( "\r\nFailed to obtain a response. The following error occurred : {0}", e->Status );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nThe following exception was raised : {0}", e->Message );
   }
}
  public static void GetPage(string url,string userName,string password,string domainName)
  {
      try
      {
          CredentialCache myCredentialCache = new CredentialCache();
          // Dummy names used as credentials.
          myCredentialCache.Add(new Uri("http://microsoft.com/"),"Basic", new NetworkCredential("user1","passwd1","domain1"));
          myCredentialCache.Add(new Uri("http://msdn.com/"),"Basic", new NetworkCredential("user2","passwd2","domain2"));
          myCredentialCache.Add(new Uri(url),"Basic", new NetworkCredential(userName,password,domainName));
          // Create a webrequest with the specified url.
       WebRequest myWebRequest = WebRequest.Create(url);
          // Call 'GetCredential' to obtain the credentials specific to our Uri.
          NetworkCredential myCredential = myCredentialCache.GetCredential(new Uri(url),"Basic");
          Display(myCredential);
       // Associating only our credentials.
          myWebRequest.Credentials = myCredential;
          // Sends the request and waits for response.
       WebResponse myWebResponse = myWebRequest.GetResponse();

          // Process response here.

       Console.WriteLine("\nResponse Received.");
          myWebResponse.Close();
      }
      catch(WebException e)
      {
          if (e.Response != null)
              Console.WriteLine("\r\nFailed to obtain a response. The following error occurred : {0}",((HttpWebResponse)(e.Response)).StatusDescription);
          else
              Console.WriteLine("\r\nFailed to obtain a response. The following error occurred : {0}",e.Status);
      }
      catch(Exception e)
      {
          Console.WriteLine("\nThe following exception was raised : {0}",e.Message);
      }
}
public static void Display(NetworkCredential credential)
{
  Console.WriteLine("\nThe credentials are:");
  Console.WriteLine("\nUsername : {0} ,Password : {1} ,Domain : {2}",credential.UserName,credential.Password,credential.Domain);
}
Public Shared Sub GetPage(url As String, userName As String, password As String, domainName As String)
    Try
        Dim myCredentialCache As New CredentialCache()
        ' Dummy names used as credentials    
        myCredentialCache.Add(New Uri("http://microsoft.com/"), "Basic", New NetworkCredential("user1", "passwd1", "domain1"))
        myCredentialCache.Add(New Uri("http://msdn.com/"), "Basic", New NetworkCredential("user2", "passwd2", "domain2"))
        myCredentialCache.Add(New Uri(url), "Basic", New NetworkCredential(userName, password, domainName))
        ' Creates a webrequest with the specified url. 
        Dim myWebRequest As WebRequest = WebRequest.Create(url)
        ' Call 'GetCredential' to obtain the credentials specific to our Uri.
        Dim myCredential As NetworkCredential = myCredentialCache.GetCredential(New Uri(url), "Basic")
        Display(myCredential)
        myWebRequest.Credentials = myCredential 'Associating only our credentials            
        ' Sends the request and waits for response.
        Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
        ' Process response here.
        Console.WriteLine(ControlChars.Cr + "Response Received.")
        myWebResponse.Close()

    Catch e As WebException
        If Not (e.Response Is Nothing) Then
            Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Failed to obtain a response. The following error occurred : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
        Else
            Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Failed to obtain a response. The following error occurred : {0}", e.Status)
        End If
    Catch e As Exception
        Console.WriteLine(ControlChars.Cr + "The following exception was raised : {0}", e.Message)
    End Try
End Sub

Public Shared Sub Display(ByVal credential As NetworkCredential)
    Console.WriteLine("The credentials are: ")
    Console.WriteLine(ControlChars.Cr + "Username : {0} ,Password : {1} ,Domain : {2}", credential.UserName, credential.Password, credential.Domain)
End Sub

설명

메서드는 GetCredential(Uri, String)CredentialCache 검색하고 지정된 URI 및 권한 부여 유형에 대한 instance 반환 NetworkCredential 합니다. 에 CredentialCache 일치하는 NetworkCredential instance null 없으면 가 반환됩니다.

GetCredential 는 캐시에서 가장 긴 일치 URI 접두사를 사용하여 권한 부여 유형에 대해 반환할 자격 증명 집합을 결정합니다. 다음 표에서는 예제를 보여 줍니다.

URI 접두사 일치하는 항목
http://www.contoso.com/portal/news.htm 특정 웹 페이지에 news.htm대한 요청입니다.
http://www.contoso.com/portal/ 페이지 news.htm를 제외한 경로의 portal 모든 콘텐츠에 대한 요청입니다.
http://www.contoso.com/ 경로에 있는 리소스를 제외한 의 www.contoso.com모든 리소스에 portal 대한 요청입니다.

적용 대상

GetCredential(String, Int32, String)

Source:
CredentialCache.cs
Source:
CredentialCache.cs
Source:
CredentialCache.cs

지정된 호스트, 포트 및 인증 프로토콜과 관련된 NetworkCredential 인스턴스를 반환합니다.

public:
 virtual System::Net::NetworkCredential ^ GetCredential(System::String ^ host, int port, System::String ^ authenticationType);
public System.Net.NetworkCredential GetCredential (string host, int port, string authenticationType);
public System.Net.NetworkCredential? GetCredential (string host, int port, string authenticationType);
abstract member GetCredential : string * int * string -> System.Net.NetworkCredential
override this.GetCredential : string * int * string -> System.Net.NetworkCredential
Public Function GetCredential (host As String, port As Integer, authenticationType As String) As NetworkCredential

매개 변수

host
String

호스트 컴퓨터를 식별하는 String입니다.

port
Int32

host에 연결할 포트를 지정하는 Int32입니다.

authenticationType
String

host에 연결할 때 사용되는 인증 체계를 식별하는 String입니다.

반환

NetworkCredential이거나 캐시에 일치하는 자격 증명이 없는 경우에는 null입니다.

구현

예외

hostnull입니다.

또는

authType이(가) null인 경우

authType이 허용되는 값이 아닌 경우.

또는

host가 빈 문자열("")인 경우

port가 0보다 작은 경우

설명

이 메서드는 CredentialCache 를 검색하고 지정된 호스트, 포트 및 권한 부여 유형에 대한 instance 반환 NetworkCredential 합니다. host이 메서드에 전달된 , portauthType 값은 메서드를 사용하여 Add 자격 증명이 에 추가 CredentialCache 될 때 지정된 값과 대/소문자를 구분하지 않습니다.

에 지원 authType 되는 값은 "NTLM", "Digest", "Kerberos" 및 "Negotiate"입니다.

적용 대상