Authentication using Web Services
The most complex part with TP services is authentication. It is recommended to create TpPolicy class and use it for authentication. Just copy the complete class code below.
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Design;
using Microsoft.Web.Services3.Security;
using Microsoft.Web.Services3.Security.Tokens;
namespace TpIntegration
{
public class TpPolicy : Policy
{
public TpPolicy()
{
Assertions.Add(new UsernameOverTransportAssertion());
}
public static UsernameToken GetUsernameToken(string username, string password, PasswordOption passwordOption)
{
UsernameToken token = new UsernameToken(username, password, passwordOption);
ISecurityTokenManager securityTokenManager =
SecurityTokenManager.GetSecurityTokenManagerByTokenType(WSTrust.TokenTypes.UsernameToken);
securityTokenManager.CacheSecurityToken(token);
return token;
}
public static void ApplyAutheticationTicket(WebServicesClientProtocol protocol, string userName, string password)
{
UsernameToken token = GetUsernameToken(userName, password, PasswordOption.SendPlainText);
protocol.SetClientCredential(token);
protocol.SetPolicy(new TpPolicy());
}
}
}
Now we are ready to investigate power of TargetProcess web services API.
using System;
using TpIntegration.UserService;
namespace TpIntegration
{
internal class Program
{
private static void Main(string[] args)
{
// Create web service and path authentication.
// "admin" / "admin" is login/password of user
// that has access to TargetProcess
UserServiceWse userService = new UserServiceWse();
TpPolicy.ApplyAutheticationTicket(userService, "admin", "admin");
// Retrieve all users in TargetProcess
UserDTO[] users = userService.RetrieveAll();
// Make something interesting with users list.
// For example, just show users
foreach (UserDTO user in users)
{
Console.WriteLine(user.FirstName + " " + user.LastName);
}
}
}
}