UsernameToken Class

Represents security credentials in the form of a user name and/or password.

Namespace: Microsoft.Web.Services2.Security.Tokens
Assembly: Microsoft.Web.Services2 (in microsoft.web.services2.dll)

Usage

'Usage
Dim element As XmlElement
Dim usernameToken1 As New UsernameToken(element)

Syntax

'Declaration
Public Class UsernameToken
    Inherits SecurityToken
    Implements IMutableSecurityToken, IDerivableToken
public class UsernameToken : SecurityToken, IMutableSecurityToken, IDerivableToken
public ref class UsernameToken : public SecurityToken, IMutableSecurityToken, IDerivableToken
public class UsernameToken extends SecurityToken implements IMutableSecurityToken, IDerivableToken
public class UsernameToken extends SecurityToken implements IMutableSecurityToken, , IDerivableToken

Example

The following code example digitally signs a SOAP message using a UsernameToken.

' Create a UsernameToken security token.
Dim userToken As UsernameToken
userToken = New UsernameToken(username, passwordEquivalent, PasswordOption.SendHashed)
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

    ' Add the UsernameToken token to the SOAP header.
    requestContext.Security.Tokens.Add(userToken)
    ' Sign the SOAP message using the UsernameToken.
    requestContext.Security.Elements.Add(New MessageSignature(userToken))

    ' Call the Web service method.
    Dim sum As Integer = serviceProxy.AddInt(a, b)

    ' If code execution reaches here, the Web service method was
    ' 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
// Create a UsernameToken security token.
UsernameToken userToken;            
userToken = new UsernameToken(username, passwordEquivalent, PasswordOption.SendHashed);

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;
   
    // Add the UsernameToken token to the SOAP message.
    requestContext.Security.Tokens.Add(userToken);

    // Sign the SOAP message using the UsernameToken.
    requestContext.Security.Elements.Add(new MessageSignature(userToken));

    // 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;
}

Remarks

A UsernameToken can be used to sign or encrypt a SOAP message.

Inheritance Hierarchy

System.Object
   Microsoft.Web.Services2.Security.Tokens.SecurityToken
    Microsoft.Web.Services2.Security.Tokens.UsernameToken

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, and Windows 2000

Target Platforms

Windows 2000, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, Pocket PC, Windows CE, Smart Phone

See Also

Reference

Microsoft.Web.Services2.Security.Tokens Namespace

Other Resources

UsernameToken Members