←Select platform

UserToken Property

Summary

User token associated with the document.

Syntax
C#
C++/CLI
Python
public string UserToken {get; set;} 
public:  
   property String^ UserToken 
   { 
      String^ get() 
      void set(String^ value) 
   } 
UserToken # get and set (LoadDocumentOptions) 

Property Value

The user token associated with the document. The default value is null.

Remarks

User tokens can optionally be used with a document to restrict usage. Refer to Loading Using LEADTOOLS Document Library for more information.

Example

This example will load a document from a URL into the cache and associate it with a user token. It will then try to load the document with and without a user token and show its behavior.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void UserTokenExample() 
{ 
   var cache = GetCache(); 
 
   var documentUri = new Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   const string documentId = "document-with-token"; 
   const string userToken = "my-secret"; 
 
   // Load from the URI without a token 
   var loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.Cache = cache; 
   loadDocumentOptions.DocumentId = documentId; 
   Console.WriteLine("Loading a document into the cache without a user token"); 
   using (var document = DocumentFactory.LoadFromUri(documentUri, loadDocumentOptions)) 
   { 
      // Make sure it does not have a user token 
      Assert.IsNull(document.UserToken); 
 
      document.AutoSaveToCache = false; 
      document.AutoDeleteFromCache = false; 
      document.SaveToCache(); 
   } 
 
   // Load from cache 
   var loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.Cache = cache; 
   loadFromCacheOptions.DocumentId = documentId; 
   loadFromCacheOptions.UserToken = null; 
 
   // This should work 
   Console.WriteLine("Loading the document from the cache without a user token"); 
   using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      // Make sure it does not have a user token 
      Assert.IsNotNull(document); 
   } 
 
   // Load it again and set the token 
   Console.WriteLine("Setting the document user token and saving it into the cache"); 
   using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      // Make sure it does not have a user token 
      Assert.IsNull(document.UserToken); 
 
      // Set a token 
      document.UserToken = userToken; 
      document.SaveToCache(); 
   } 
 
   // This should fail 
   Console.WriteLine("Loading the document without a user token"); 
   using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      // Make sure it did not load 
      Assert.IsNull(document); 
   } 
 
   // This should work 
   loadFromCacheOptions.UserToken = userToken; 
   Console.WriteLine("Loading the document with a user token"); 
   using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) 
   { 
      // Make sure it has the same user token 
      Assert.IsNotNull(document); 
      Assert.AreEqual(userToken, document.UserToken); 
   } 
 
   // This should fail 
   loadFromCacheOptions.UserToken = null; 
   Console.WriteLine("Deleting the document without a user token"); 
   bool isDeletedFromCache = DocumentFactory.DeleteFromCache(loadFromCacheOptions); 
   // Make sure it did not get deleted 
   Assert.IsFalse(isDeletedFromCache); 
 
   // This should work 
   loadFromCacheOptions.UserToken = userToken; 
   Console.WriteLine("Deleting the document with a user token"); 
   isDeletedFromCache = DocumentFactory.DeleteFromCache(loadFromCacheOptions); 
   // Make sure it got deleted 
   Assert.IsTrue(isDeletedFromCache); 
} 
 
 
public ObjectCache GetCache() 
{ 
   // Create a LEADTOOLS FileCache object 
 
   var cacheDir = Path.Combine(LEAD_VARS.ImagesDir, "cache"); 
   if (Directory.Exists(cacheDir)) 
      Directory.Delete(cacheDir, true); 
 
   Directory.CreateDirectory(cacheDir); 
 
   var cache = new FileCache(); 
   cache.CacheDirectory = cacheDir; 
 
   return cache; 
} 
 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void userTokenExample() throws URISyntaxException { 
   FileCache cache = getCache(); 
 
   URI documentUri = new URI("https://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   final String documentId = "document-with-token"; 
   final String userToken = "my-secret"; 
 
   // Load from the URI without a token 
   LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.setCache(cache); 
   loadDocumentOptions.setDocumentId(documentId); 
   System.out.println("Loading a document into the cache without a user token"); 
   LEADDocument document = DocumentFactory.loadFromUri(documentUri, loadDocumentOptions); 
   // Make sure it does not have a user token 
   assertTrue(document.getUserToken() == null); 
 
   document.setAutoSaveToCache(false); 
   document.setAutoDeleteFromCache(false); 
   document.saveToCache(); 
 
   // Load from cache 
   LoadFromCacheOptions loadFromCacheOptions = new LoadFromCacheOptions(); 
   loadFromCacheOptions.setCache(cache); 
   loadFromCacheOptions.setDocumentId(documentId); 
   loadFromCacheOptions.setUserToken(null); 
 
   // This should work 
   System.out.println("Loading the document from the cache without a user token"); 
   document = DocumentFactory.loadFromCache(loadFromCacheOptions); 
   // Make sure it does not have a user token 
   assertTrue(document != null); 
 
   // Load it again and set the token 
   System.out.println("Setting the document user token and saving it into the cache"); 
   document = DocumentFactory.loadFromCache(loadFromCacheOptions); 
 
   // Make sure it does not have a user token 
   assertTrue(document.getUserToken() == null); 
 
   // Set a token 
   document.setUserToken(userToken); 
   document.saveToCache(); 
 
   // This should fail 
   System.out.println("Loading the document without a user token"); 
   document = DocumentFactory.loadFromCache(loadFromCacheOptions); 
 
   // Make sure it did not load 
   assertTrue(document == null); 
 
   // This should work 
   loadFromCacheOptions.setUserToken(userToken); 
   System.out.println("Loading the document with a user token"); 
   document = DocumentFactory.loadFromCache(loadFromCacheOptions); 
   // Make sure it has the same user token 
   assertTrue(document != null); 
   assertTrue(userToken.equals(document.getUserToken())); 
 
   // This should fail 
   loadFromCacheOptions.setUserToken(null); 
   System.out.println("Deleting the document without a user token"); 
   boolean isDeletedFromCache = DocumentFactory.deleteFromCache(loadFromCacheOptions); 
   // Make sure it did not get deleted 
   assertFalse(isDeletedFromCache); 
 
   // This should work 
   loadFromCacheOptions.setUserToken(userToken); 
   System.out.println("Deleting the document with a user token"); 
   isDeletedFromCache = DocumentFactory.deleteFromCache(loadFromCacheOptions); 
   // Make sure it got deleted 
   assertTrue(isDeletedFromCache); 
 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.