Share via


AuthenticationManager.CustomTargetNameDictionary 屬性

定義

取得包含服務主要名稱 (SPN) 的字典,在使用 Kerberos 驗證要求 (使用 WebRequest 及其衍生類別所提出) 期間,這些服務主要名稱用來識別主機。

public:
 static property System::Collections::Specialized::StringDictionary ^ CustomTargetNameDictionary { System::Collections::Specialized::StringDictionary ^ get(); };
public static System.Collections.Specialized.StringDictionary CustomTargetNameDictionary { get; }
static member CustomTargetNameDictionary : System.Collections.Specialized.StringDictionary
Public Shared ReadOnly Property CustomTargetNameDictionary As StringDictionary

屬性值

可寫入的 StringDictionary,包含由主機資訊所組成之索引鍵的 SPN 值。

範例

下列程式代碼範例示範顯示 的內容 CustomTargetNameDictionary

static void RequestResource( Uri^ resource )
{
   // Set policy to send credentials when using HTTPS and basic authentication.
   // Create a new HttpWebRequest object for the specified resource.
   WebRequest^ request = dynamic_cast<WebRequest^>(WebRequest::Create( resource ));

   // Supply client credentials for basic authentication.
   request->UseDefaultCredentials = true;
   request->AuthenticationLevel = AuthenticationLevel::MutualAuthRequired;
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

   // Determine mutual authentication was used.
   Console::WriteLine( L"Is mutually authenticated? {0}", response->IsMutuallyAuthenticated );
   System::Collections::Specialized::StringDictionary^ spnDictionary = AuthenticationManager::CustomTargetNameDictionary;
   System::Collections::IEnumerator^ myEnum = spnDictionary->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry^ e = safe_cast<DictionaryEntry^>(myEnum->Current);
      Console::WriteLine( "Key: {0}  - {1}", dynamic_cast<String^>(e->Key), dynamic_cast<String^>(e->Value) );
   }

   // Read and display the response.
   System::IO::Stream^ streamResponse = response->GetResponseStream();
   System::IO::StreamReader^ streamRead = gcnew System::IO::StreamReader( streamResponse );
   String^ responseString = streamRead->ReadToEnd();
   Console::WriteLine( responseString );

   // Close the stream objects.
   streamResponse->Close();
   streamRead->Close();

   // Release the HttpWebResponse.
   response->Close();
}

/*

The output from this example will differ based on the requested resource
and whether mutual authentication was successful. For the purpose of illustration,
a sample of the output is shown here:

Is mutually authenticated? True
Key: http://server1.someDomain.contoso.com  - HTTP/server1.someDomain.contoso.com

<html>
...
</html>

*/
       public static void RequestResource(Uri resource)
        {
            // Set policy to send credentials when using HTTPS and basic authentication.

            // Create a new HttpWebRequest object for the specified resource.
            WebRequest request=(WebRequest) WebRequest.Create(resource);
            // Supply client credentials for basic authentication.
            request.UseDefaultCredentials = true;
            request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequired;
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            // Determine mutual authentication was used.
            Console.WriteLine("Is mutually authenticated? {0}", response.IsMutuallyAuthenticated);

             System.Collections.Specialized.StringDictionary spnDictionary = AuthenticationManager.CustomTargetNameDictionary;
            foreach (System.Collections.DictionaryEntry e in spnDictionary)
            {
                Console.WriteLine("Key: {0}  - {1}", e.Key as string, e.Value as string);
            }
            // Read and display the response.
            System.IO.Stream streamResponse = response.GetResponseStream();
            System.IO.StreamReader streamRead = new System.IO.StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();
            Console.WriteLine(responseString);
            // Close the stream objects.
            streamResponse.Close();
            streamRead.Close();
            // Release the HttpWebResponse.
            response.Close();
        }

/*

The output from this example will differ based on the requested resource
and whether mutual authentication was successful. For the purpose of illustration,
a sample of the output is shown here:

Is mutually authenticated? True
Key: http://server1.someDomain.contoso.com  - HTTP/server1.someDomain.contoso.com

<html>
...
</html>

*/

備註

SPN 是客戶端為了相互驗證而唯一識別伺服器上服務或應用程式實例的名稱。 默認會要求相互驗證,而且您可以在要求中將 設定WebRequest.AuthenticationLevelMutualAuthRequired為 來要求它。

WebRequest當 需要相互驗證時,目的地的SPN必須由用戶端提供。 如果您知道 SPN,您可以在傳送要求之前將它新增至 CustomTargetNameDictionary 。 如果您尚未將SPN資訊新增至此字典,則會AuthenticationManagerRequestUri使用 方法來撰寫最有可能的SPN;不過,這是計算的值,而且可能不正確。 如果嘗試進行相互驗證且失敗,您可以檢查字典來判斷計算的SPN。 如果驗證通訊協定不支援相互驗證,就不會在字典中輸入SPN。

若要將SPN值新增至這個字典,請使用 AbsoluteUriRequestUri 作為索引鍵。 在內部,會截斷索引鍵以包含 SchemeHostPort ,如果不是預設埠,則為 。

注意

存取 的方法和屬性 CustomTargetNameDictionary 需要不受限制 WebPermission

注意

透過 Proxy 執行 Kerberos 驗證時,Proxy 和最終主機名都必須解析為 SPN。 Proxy 名稱解析受到逾時保護。 將最終主機名解析為SPN需要 DNS 查閱,而且沒有與這項作業直接相關聯的逾時。 因此,同步作業可能需要較長的時間才能逾時。 若要克服這個問題,請先將最終主機的 URI 前置詞新增至 SPN 快取,再對它提出要求。

如果未設定 CustomTargetNameDictionary 屬性,版本 3.5 SP1 現在預設成指定 NTLM (NT LAN Manager) 驗證交換時 SPN 之要求 URL 中所使用的主機名稱。 要求 URL 中所使用的主機名稱可能不同於用戶端要求的 System.Net.HttpRequestHeader 中所指定的主機標頭。 要求 URL 中所使用的主機名稱可能不同於伺服器的實際主機名稱、伺服器的電腦名稱、電腦的 IP 位址或迴路位址。 在這些情況下,Windows 會讓驗證要求失敗。 若要解決此問題,您可能需要通知 Windows 用戶端要求 (“contoso” 中要求 URL 中使用的主機名,例如,) 實際上是本機計算機的替代名稱。

適用於

另請參閱