CredentialCache.GetCredential メソッド
指定した URI と認証の種類に関連付けられた NetworkCredential インスタンスを返します。
Public Overridable Function GetCredential( _
ByVal uriPrefix As Uri, _ ByVal authType As String _) As NetworkCredential Implements ICredentials.GetCredential
[C#]
public virtual NetworkCredential GetCredential(UriuriPrefix,stringauthType);
[C++]
public: virtual NetworkCredential* GetCredential(Uri* uriPrefix,String* authType);
[JScript]
public function GetCredential(
uriPrefix : Uri,authType : String) : NetworkCredential;
パラメータ
- uriPrefix
資格情報によってアクセスが許可されるリソースの URI プリフィックスを指定する Uri 。 - authType
uriPrefix で指定されたリソースが使用する認証方式。
戻り値
NetworkCredential 。または、キャッシュに一致する資格情報がない場合は null 参照 (Visual Basic では Nothing) 。
実装
解説
GetCredential メソッドは、 CredentialCache を検索し、指定した URI と認証の種類の NetworkCredential インスタンスを返します。 CredentialCache に一致する NetworkCredential インスタンスがない場合は、 null 参照 (Visual Basic では Nothing) が返されます。
GetCredential は、キャッシュ内の一致する最も長い URI プリフィックスを使用して、認証の種類に対して返す資格情報のセットを決定します。例を次の表に示します。
URI プリフィックス | 一致 |
---|---|
https://www.contoso.com/portal/news.htm | 特定の Web ページ news.htm の要求。 |
https://www.contoso.com/portal/ | ページ news.htm を除く、 portal パスのすべての内容に対する要求。 |
https://www.contoso.com/ | portal パスを除く、 www.contoso.com のすべてのリソースに対する要求。 |
使用例
[Visual Basic, C#, C++] GetCredential メソッドを使用して、指定した URI および認証の種類に関連付けられている NetworkCredential インスタンスを返す例を次に示します。
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("https://microsoft.com/"), "Basic", New NetworkCredential("user1", "passwd1", "domain1"))
myCredentialCache.Add(New Uri("https://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 occured : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Else
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "Failed to obtain a response. The following error occured : {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 'GetPage
Public Shared Sub Display(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 'Display
[C#]
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("https://microsoft.com/"),"Basic", new NetworkCredential("user1","passwd1","domain1"));
myCredentialCache.Add(new Uri("https://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 occured : {0}",((HttpWebResponse)(e.Response)).StatusDescription);
else
Console.WriteLine("\r\nFailed to obtain a response. The following error occured : {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);
}
[C++]
void Display(NetworkCredential* credential)
{
Console::WriteLine(S"\nThe credentials are:");
Console::WriteLine(S"\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 = new CredentialCache();
// Dummy names used as credentials.
myCredentialCache->Add(new Uri(S"https://microsoft.com/"),
S"Basic", new NetworkCredential(S"user1", S"passwd1", S"domain1"));
myCredentialCache->Add(new Uri(S"https://msdn.com/"), S"Basic",
new NetworkCredential(S"user2", S"passwd2", S"domain2"));
myCredentialCache->Add(new Uri(url), S"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), S"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(S"\nResponse Received.");
myWebResponse->Close();
}
catch (WebException* e)
{
if (e->Response != 0)
Console::WriteLine(S"\r\nFailed to obtain a response. The following error occured : {0}",
(dynamic_cast<HttpWebResponse*>(e->Response))->StatusDescription);
else
Console::WriteLine(S"\r\nFailed to obtain a response. The following error occured : {0}",
__box( e->Status));
}
catch (Exception* e)
{
Console::WriteLine(S"\nThe following exception was raised : {0}", e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard