Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
When send policy is configured such that a security token is required to sign, encrypt or be added to sent SOAP messages, the sender must add a security token to the PolicyEnforcementSecurityTokenCache token cache matching the type of security token specified in the policy. For instance, if the send policy contains an <Integrity> Element element specifying that a UsernameToken must sign out-going SOAP messages, the sender must add a UsernameToken to the PolicyEnforcementSecurityTokenCache. When the SOAP message is sent, WSE queries the PolicyEnforcementSecurityTokenCache for a UsernameToken that matches the send policy requirement. If a match is found, WSE creates a UsernameToken from the UsernameToken and signs the SOAP message with it.
For details about configuring a Web service's policy, see Configuring a Web Service's Policy.
To add a security token template to the PolicyEnforcementSecurityTokenCache token cache
Create a new instance of a security token.
The following code example creates a new instance of the UsernameToken class.
Dim userToken As New UsernameToken(username, passwordEquivalent, _ PasswordOption.SendHashed)
UsernameToken userToken = new UsernameToken(username, passwordEquivalent, PasswordOption.SendHashed);
Add the security token to the PolicyEnforcementSecurityTokenCache.
The following code example adds a UsernameToken to the PolicyEnforcementSecurityTokenCache.
PolicyEnforcementSecurityTokenCache.GlobalCache.Add(userToken)
PolicyEnforcementSecurityTokenCache.GlobalCache.Add(userToken);
Example
The following code example adds a UsernameToken to the PolicyEnforcementSecurityTokenCache.
Dim username As String = Environment.UserName
Dim passwordEquivalent As String
' Create a UsernameToken security token.
Dim userToken As New UsernameToken(username, _
PasswordEquivalent, PasswordOption.SendHashed)
PolicyEnforcementSecurityTokenCache.GlobalCache.Add(userToken)
Try
' Create a new instance of the proxy class.
Dim serviceProxy As New AddNumbersWse
' Get the SoapContext associated with the SOAP request.
Dim requestContext As SoapContext = serviceProxy.RequestSoapContext
' Set the period of time in which the SOAP request expires to one
' minute.
requestContext.Security.Timestamp.TtlInSeconds = 60
' Call the Web service method.
Dim sum As Integer = serviceProxy.AddInt(a, b)
' If code execution reaches here, the Web service method
' successfully communicated with.
Dim message As String = String.Format("{0} + {1} = {2}", a, b, sum)
MessageBox.Show(message, "Success!", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch se As System.Web.Services.Protocols.SoapException
MessageBox.Show(se.ToString())
Catch ex As Exception
MessageBox.Show("Exception caught while invoking a web service.", _
ex.Message)
Return
End Try
string username = Environment.UserName;
string passwordEquivalent;
// Create a UsernameToken Security token and add it to the SecurityTokenCache.
UsernameToken userToken;
userToken = new UsernameToken(username,
passwordEquivalent, PasswordOption.SendHashed);
PolicyEnforcementSecurityTokenCache.GlobalCache.Add(userToken);
try
{
// Create a new instance of the proxy class.
AddNumbersWse serviceProxy = new AddNumbersWse();
// Get the SoapContext associated with the SOAP request.
SoapContext requestContext = serviceProxy.RequestSoapContext;
// Set the period of time in which the SOAP request expires to one
// minute.
requestContext.Security.Timestamp.TtlInSeconds = 60;
// Call the Web service method.
int sum = serviceProxy.AddInt(a, b);
// If code execution reaches here, the Web service method was
// successfully communicated with.
string message = string.Format("{0} + {1} = {2}", a, b, sum);
MessageBox.Show(message, "Success!", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (System.Web.Services.Protocols.SoapException se)
{
MessageBox.Show(se.ToString());
}
catch (Exception ex)
{
MessageBox.Show ("Exception caught while invoking a web service.",
ex.Message );
return;
}
Compiling the Code
This example requires:
- References to the Microsoft.Web.Services2, Microsoft.Web.Services2.Security.Policy, and the Microsoft.Web.Services2.Security.Tokens namespaces.
See Also
Tasks
How to: Add Security Credentials to a SOAP Message
Reference
PolicyEnforcementSecurityTokenCache