Connection grouping associates specific requests within a single application to a defined connection pool. This can be required by a middle-tier application that connects to a back-end server on behalf of a user and uses an authentication protocol that supports delegation, such as Kerberos, or by a middle-tier application that supplies its own credentials, as in the example below. For example, suppose a user, Joe, visits an internal Web site that displays his payroll information. After authenticating Joe, the middle-tier application server uses Joe's credentials to connect to the back-end server to retrieve his payroll information. Next, Susan visits the site and requests her payroll information. Because the middle-tier application has already made a connection using Joe's credentials, the back-end server responds with Joe's information. However, if the application assigns each request sent to the back-end server to a connection group formed from the user name, then each user belongs to a separate connection pool and cannot accidentally share authentication information with another user.
To assign a request to a specific connection group, you must assign a name to the ConnectionGroupName property of your WebRequest before making the request. 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.
// 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(); [Visual Basic] ' 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()
Assigning a different value to the UserName variable on subsequent requests produces a new server connection using the proper credentials.