WebRequest.AuthenticationLevel WebRequest.AuthenticationLevel WebRequest.AuthenticationLevel WebRequest.AuthenticationLevel Property

定义

获取或设置用于此请求的身份验证和模拟的级别。Gets or sets values indicating the level of authentication and impersonation used for this request.

public:
 property System::Net::Security::AuthenticationLevel AuthenticationLevel { System::Net::Security::AuthenticationLevel get(); void set(System::Net::Security::AuthenticationLevel value); };
public System.Net.Security.AuthenticationLevel AuthenticationLevel { get; set; }
member this.AuthenticationLevel : System.Net.Security.AuthenticationLevel with get, set
Public Property AuthenticationLevel As AuthenticationLevel

属性值

AuthenticationLevel 值的按位组合。A bitwise combination of the AuthenticationLevel values. 默认值为 MutualAuthRequestedThe default value is MutualAuthRequested.

在相互身份验证中,客户端和服务器双方都出示凭据以确定其身份。In mutual authentication, both the client and server present credentials to establish their identity. MutualAuthRequiredMutualAuthRequested 值与 Kerberos 身份验证有关。The MutualAuthRequired and MutualAuthRequested values are relevant for Kerberos authentication. 可以直接支持 Kerberos 身份验证,也可以仅在使用 Negotiate 安全协议选择实际的安全协议时使用该身份验证。Kerberos authentication can be supported directly, or can be used if the Negotiate security protocol is used to select the actual security protocol. 有关身份验证协议的详细信息,请参阅Internet 身份验证For more information about authentication protocols, see Internet Authentication.

若要确定相互身份验证是否发生,请检查 IsMutuallyAuthenticated 属性。To determine whether mutual authentication occurred, check the IsMutuallyAuthenticated property.

如果您指定了 MutualAuthRequired 身份验证标志值,而未发生相互身份验证,则应用程序将收到一个带有 IOException 内部异常的 ProtocolViolationException,指示相互身份验证失败。If you specify the MutualAuthRequired authentication flag value and mutual authentication does not occur, your application will receive an IOException with a ProtocolViolationException inner exception indicating that mutual authentication failed.

示例

下面的代码示例设置此属性的值。The following code example sets the value of this property.

// The following example uses the System, System.Net, 
// and System.IO namespaces.
static void RequestMutualAuth( Uri^ resource )
{
   // Create a new HttpWebRequest object for the specified resource.
   WebRequest^ request = dynamic_cast<WebRequest^>(WebRequest::Create( resource ));

   // Request mutual authentication.
   request->AuthenticationLevel = AuthenticationLevel::MutualAuthRequested;

   // Supply client credentials.
   request->Credentials = CredentialCache::DefaultCredentials;
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

   // Determine whether mutual authentication was used.
   Console::WriteLine( L"Is mutually authenticated? {0}", response->IsMutuallyAuthenticated );

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

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

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

// The following example uses the System, System.Net, 
// and System.IO namespaces.

public static void RequestMutualAuth(Uri resource)
{
    // Create a new HttpWebRequest object for the specified resource.
    WebRequest request=(WebRequest) WebRequest.Create(resource);
    // Request mutual authentication.
   request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
    // Supply client credentials.
    request.Credentials = CredentialCache.DefaultCredentials;
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    // Determine whether mutual authentication was used.
    Console.WriteLine("Is mutually authenticated? {0}", response.IsMutuallyAuthenticated);
    // Read and display the response.
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
   Console.WriteLine(responseString);
    // Close the stream objects.
    streamResponse.Close();
    streamRead.Close();
    // Release the HttpWebResponse.
    response.Close();
}

适用于