Como: Atribuir informações de usuário a conexões de grupoHow to: Assign User Information to Group Connections

O exemplo a seguir demonstra como atribuir informações de usuário a conexões de grupo, supondo que o aplicativo defina as variáveis UserName, SecurelyStoredPassword e Domain antes que essa seção do código seja chamada e que UserName seja exclusivo.The following example demonstrates how to assign user information to group connections, assuming that the application sets the variables UserName, SecurelyStoredPassword, and Domain before this section of code is called and that UserName is unique.

Para atribuir informações de usuário a uma conexão de grupoTo assign user information to a group connection

  1. Crie um nome de grupo de conexão.Create a connection group name.

    SHA1Managed Sha1 = new SHA1Managed();  
    Byte[] updHash = Sha1.ComputeHash(Encoding.UTF8.GetBytes(UserName + SecurelyStoredPassword + Domain));  
    String secureGroupName = Encoding.Default.GetString(updHash);  
    
    Dim Sha1 As New SHA1Managed()  
    Dim updHash As [Byte]() = Sha1.ComputeHash(Encoding.UTF8.GetBytes((UserName + SecurelyStoredPassword + Domain)))  
    Dim secureGroupName As [String] = Encoding.Default.GetString(updHash)  
    
  2. Crie uma solicitação para uma URL específica.Create a request for a specific URL. Por exemplo, o código a seguir cria uma solicitação para a URL http://www.contoso.com.For example, the following code creates a request for the URL http://www.contoso.com.

    WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");  
    
    Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")  
    
  3. Defina as credenciais e o GroupName da Conexão para a solicitação da Web e chame GetResponse para recuperar um objeto WebResponse.Set the credentials and Connection GroupName for the Web request, and call GetResponse to retrieve a WebResponse object.

    myWebRequest.Credentials = new NetworkCredential(UserName, SecurelyStoredPassword, Domain);
    myWebRequest.ConnectionGroupName = secureGroupName;  
    
    WebResponse myWebResponse=myWebRequest.GetResponse();  
    
    myWebRequest.Credentials = New NetworkCredential(UserName, SecurelyStoredPassword, Domain)  
    myWebRequest.ConnectionGroupName = secureGroupName  
    
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()  
    
  4. Feche o fluxo de resposta depois de usar o objeto WebRespose.Close the response stream after using the WebRespose object.

    MyWebResponse.Close();  
    
    MyWebResponse.Close()  
    

ExemploExample

// Create a connection group name.  
SHA1Managed Sha1 = new SHA1Managed();  
Byte[] updHash = Sha1.ComputeHash(Encoding.UTF8.GetBytes(UserName + SecurelyStoredPassword + Domain));  
String secureGroupName = Encoding.Default.GetString(updHash);  
  
// Create a request for a specific URL.  
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");  
  
myWebRequest.Credentials = new NetworkCredential(UserName, SecurelyStoredPassword, Domain);
myWebRequest.ConnectionGroupName = secureGroupName;  
  
WebResponse myWebResponse=myWebRequest.GetResponse();  
  
// Insert the code that uses myWebResponse.  
  
MyWebResponse.Close();  
' Create a secure group name.  
Dim Sha1 As New SHA1Managed()  
Dim updHash As [Byte]() = Sha1.ComputeHash(Encoding.UTF8.GetBytes((UserName + SecurelyStoredPassword + Domain)))  
Dim secureGroupName As [String] = Encoding.Default.GetString(updHash)  
  
' Create a request for a specific URL.  
Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")  
  
myWebRequest.Credentials = New NetworkCredential(UserName, SecurelyStoredPassword, Domain)  
myWebRequest.ConnectionGroupName = secureGroupName  
  
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()  
  
' Insert the code that uses myWebResponse.  
MyWebResponse.Close()  

Consulte tambémSee also