Click or drag to resize
Authentication using an existing token

In some situations a developer may want to create a separation of concerns whereby one module might be responsible for obtaining authentication tokens from the Workspaces server while other components use those tokens to communicate with the Workspaces server. The Workspaces SDK provides a means to do this using the LoadExistingSession method on ApiSession.

In the module that provides the authentication tokens there would be some code, similar to the snippet below, which obtains a valid authentication token for the Workspaces server.

pubic String ObtainTokenFromSeparateModule(String username)
{
  String authToken = String.Empty;

  String serviceAccountName = "com.watchdox.system.xxxx.yyyy";
  int expiresInMinutes = 5;
  X509Certificate2 certificate = new X509Certificate2("myCertFile.crt", "certPassword");

  ApiSession apiSession = new ApiSession(serverUrl);

  LoginResult loginResult =
    apiSession.StartSessionWithServiceAccount(username,
                                              serviceAccountName,
                                              expiresInMinutes,
                                              certificate);

  if (loginResult == LoginResult.SUCCESS) {
    authToken = apiSession.GetToken();
  }

  return authToken;
}

Having obtained an authentiation token from the other module, it would then passed to the LoadExistingSession method of an ApiSession instance.

String authToken = ObtainTokenFromSeparateModule();

if (!string.IsNullOrEmpty(authToken))
{
  ApiSession apiSession = new ApiSession(serverUrl);
  LoginResult loginResult = apiSession.LoadExistingSession(authToken);
}