UsernameToken Constructor (String, String, PasswordOption)
Initializes a new instance of the UsernameToken class with the specified user name, password, and password sending option.
Namespace: Microsoft.Web.Services2.Security.Tokens
Assembly: Microsoft.Web.Services2 (in microsoft.web.services2.dll)
Usage
'Usage
Dim username As String
Dim password As String
Dim passwordOption As PasswordOption
Dim usernameToken1 As New UsernameToken(username, password, passwordOption)
Syntax
'Declaration
Public Sub New( _
ByVal username As String, _
ByVal password As String, _
ByVal passwordOption As PasswordOption _
)
public UsernameToken(
string username,
string password,
PasswordOption passwordOption
);
public: UsernameToken(
String^ username,
String^ password,
PasswordOption^ passwordOption
);
public UsernameToken(
System.String username,
System.String password,
PasswordOption passwordOption
);
public function UsernameToken(
username : String,
password : String,
passwordOption : PasswordOption
);
Parameters
- username
The user name associated with the security token.
- password
The password for the user name associated with the security token.
- passwordOption
One of the PasswordOption values specifying how the password is sent in SOAP messages.
Example
The following code example signs a SOAP message using a UsernameToken, specifying that the SHA-1 hash of the password be sent in the SOAP message.
' 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;
}
Exceptions
| Exception type | Condition |
|---|---|
| System.ArgumentNullException | username is null. -or- username is an empty string (""). -or- password is null. -or- password is an empty string (""). |
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
UsernameToken Class
Microsoft.Web.Services2.Security.Tokens Namespace