Exportar (0) Imprimir
Expandir todo

IAuthenticationModule.Authenticate (Método)

Devuelve una instancia de la clase Authorization en respuesta a un desafío de autenticación de un servidor.

Espacio de nombres: System.Net
Ensamblado: System (en system.dll)

Authorization Authenticate (
	string challenge,
	WebRequest request,
	ICredentials credentials
)
Authorization Authenticate (
	String challenge, 
	WebRequest request, 
	ICredentials credentials
)
function Authenticate (
	challenge : String, 
	request : WebRequest, 
	credentials : ICredentials
) : Authorization

Parámetros

challenge

Desafío de autenticación enviado por el servidor.

request

Instancia de WebRequest asociada al desafío.

credentials

Credenciales asociadas al desafío.

Valor devuelto

Instancia de Authorization que contiene el mensaje de autorización para la solicitud, o referencia de objeto null (Nothing en Visual Basic) si no puede controlarse el desafío.

El método Authenticate realiza el proceso de autenticación con el servidor y devuelve una instancia de Authorization a AuthenticationManager.

Notas para los implementadores AuthenticationManager llama al método Authenticate en los módulos de autenticación registrados para determinar qué módulo controla el desafío. Si el módulo de autenticación no puede controlar el desafío, el método Authenticate tiene que devolver referencia de objeto null (Nothing en Visual Basic). Si el módulo de autenticación encuentra un error a medida que realiza el proceso de autenticación, Authenticate tiene que producir una excepción.

En el siguiente ejemplo, se muestra cómo utilizar el método Authenticate. Para obtener un ejemplo completo, vea la clase AuthenticationManager.

// Authenticate is the core method for this custom authentication.
// When an Internet resource requests authentication, the WebRequest.GetResponse 
// method calls the AuthenticationManager.Authenticate method. This method, in 
// turn, calls the Authenticate method on each of the registered authentication
// modules, in the order in which they were registered. When the authentication is 
// complete an Authorization object is returned to the WebRequest.
public Authorization Authenticate(String challenge, WebRequest request, ICredentials credentials) 
{
  Encoding ASCII = Encoding.ASCII;        

  // Get the username and password from the credentials
  NetworkCredential MyCreds = credentials.GetCredential(request.RequestUri, "Basic");        

  if (PreAuthenticate(request, credentials) == null)
    Console.WriteLine("\n Pre-authentication is not allowed.");
  else
    Console.WriteLine("\n Pre-authentication is allowed.");

  // Verify that the challenge satisfies the authorization requirements.
  bool challengeOk = checkChallenge(challenge, MyCreds.Domain);

  if (!challengeOk)
    return null;

  // Create the encrypted string according to the Basic authentication format as
  // follows:
  // a)Concatenate the username and password separated by colon;
  // b)Apply ASCII encoding to obtain a stream of bytes;
  // c)Apply Base64 encoding to this array of bytes to obtain the encoded 
  // authorization.
  string BasicEncrypt = MyCreds.UserName + ":" + MyCreds.Password;

  string BasicToken = "Basic " + Convert.ToBase64String(ASCII.GetBytes(BasicEncrypt));

  // Create an Authorization object using the encoded authorization above.
  Authorization resourceAuthorization = new Authorization(BasicToken);

  // Get the Message property, which contains the authorization string that the 
  // client returns to the server when accessing protected resources.
  Console.WriteLine("\n Authorization Message:{0}",resourceAuthorization.Message);

  // Get the Complete property, which is set to true when the authentication process 
  // between the client and the server is finished.
  Console.WriteLine("\n Authorization Complete:{0}",resourceAuthorization.Complete);

  Console.WriteLine("\n Authorization ConnectionGroupId:{0}",resourceAuthorization.ConnectionGroupId);


  return resourceAuthorization;
}

// Authenticate is the core method for this custom authentication.
// When an Internet resource requests authentication, the WebRequest.
// GetResponse  method calls the AuthenticationManager.Authenticate method. 
// This method, in  turn, calls the Authenticate method on each of the 
// registered authentication modules, in the order in which they were 
// registered. When the authentication is  complete an Authorization object 
// is returned to the WebRequest.
public Authorization Authenticate(
    String challenge, WebRequest request, ICredentials credentials)
{
    Encoding ascii = Encoding.get_ASCII();

    // Get the username and password from the credentials
    NetworkCredential myCreds = credentials.GetCredential(
                                request.get_RequestUri(), "Basic");
    if (PreAuthenticate(request, credentials) == null) {
        Console.WriteLine("\n Pre-authentication is not allowed.");
    }
    else {
        Console.WriteLine("\n Pre-authentication is allowed.");
    }

    // Verify that the challenge satisfies the authorization requirements.
    boolean challengeOk = CheckChallenge(challenge, myCreds.get_Domain());

    if (!(challengeOk)) {
        return null;
    }
    // Create the encrypted string according to the Basic authentication 
    // format as follows:
    // a)Concatenate the username and password separated by colon;
    // b)Apply ascii encoding to obtain a stream of bytes;
    // c)Apply Base64 encoding to this array of bytes to obtain the encoded 
    // authorization.
    String basicEncrypt = myCreds.get_UserName() + ":" 
        + myCreds.get_Password();
    String basicToken = "Basic " 
        + Convert.ToBase64String(ascii.GetBytes(basicEncrypt));

    // Create an Authorization object using the encoded
    // authorization above.
    Authorization resourceAuthorization = new Authorization(basicToken);

    // Get the Message property, which contains the authorization string 
    // that the client returns to the server when accessing protected 
    // resources.
    Console.WriteLine("\n Authorization Message:{0}", 
        resourceAuthorization.get_Message());

    // Get the Complete property, which is set to true when the 
    // authentication process between the client and the
    // server is finished.
    Console.WriteLine("\n Authorization Complete:{0}", 
        System.Convert.ToString(resourceAuthorization.get_Complete()));

    Console.WriteLine("\n Authorization ConnectionGroupId:{0}", 
        resourceAuthorization.get_ConnectionGroupId());
    return resourceAuthorization;
} //Authenticate

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0

Adiciones de comunidad

AGREGAR
Mostrar:
© 2015 Microsoft