Udostępnij za pośrednictwem


How to: Assign User Information to Group Connections

Code Sample

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.

To assign user information to a group connection

  1. 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. Create a request for a specific URL. For example, the following code creates a request for the URL https://www.contoso.com.

    WebRequest myWebRequest=WebRequest.Create("https://www.contoso.com");
    
    Dim myWebRequest As WebRequest = WebRequest.Create("https://www.contoso.com")
    
  3. 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. Close the response stream after using the WebRespose object.

    MyWebResponse.Close();
    
    MyWebResponse.Close()
    

Example

// 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("https://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("https://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()

See Also

Concepts

Managing Connections

Connection Grouping