←Select platform

UserToken Property

Summary

User token associated with the document.

Syntax
C#
VB
C++
public string UserToken {get; set;} 
Public Property UserToken() As String 
   Get 
   Set 
public:  
   property String^ UserToken 
   { 
      String^ get() 
      void set(String^ value) 
   } 

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#
VB
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:\LEADTOOLS21\Resources\Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Document.Writer 
Imports Leadtools.Svg 
Imports Leadtools.Document 
Imports Leadtools.Caching 
Imports Leadtools.Annotations.Engine 
Imports Leadtools.Barcode 
Imports Leadtools.Ocr 
Imports LeadtoolsDocumentExamples.LeadtoolsExamples.Common 
Imports Leadtools.Document.Converter 
 
Public Shared Sub UserTokenExample() 
   Dim cache As ObjectCache = GetCache() 
 
   Dim documentUri As New Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf") 
   Const documentId As String = "document-with-token" 
   Const userToken As String = "my-secret" 
 
   ' Load from the URI without a token 
   Dim loadDocumentOptions As New LoadDocumentOptions() 
   loadDocumentOptions.Cache = cache 
   loadDocumentOptions.DocumentId = documentId 
   Console.WriteLine("Loading a document into the cache without a user token") 
   Using document As LEADDocument = 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() 
   End Using 
 
   ' Load from cache 
   Dim loadFromCacheOptions As New LoadFromCacheOptions() 
   loadFromCacheOptions.Cache = cache 
   loadFromCacheOptions.DocumentId = documentId 
   loadFromCacheOptions.UserToken = Nothing 
 
   ' This should work 
   Console.WriteLine("Loading the document from the cache without a user token") 
   Using document As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions) 
      ' Make sure it does Not have a user token 
      Assert.IsNotNull(document) 
   End Using 
 
   ' Load it again And set the token 
   Console.WriteLine("Setting the document user token and saving it into the cache") 
   Using document As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions) 
      ' Make sure it does Not have a user token 
      Assert.IsNull(document.UserToken) 
 
      ' Set a token 
      document.UserToken = userToken 
      document.SaveToCache() 
   End Using 
 
   ' This should fail 
   Console.WriteLine("Loading the document without a user token") 
   Using document As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions) 
      ' Make sure it did Not load 
      Assert.IsNull(document) 
   End Using 
 
   ' This should work 
   loadFromCacheOptions.UserToken = userToken 
   Console.WriteLine("Loading the document with a user token") 
   Using document As LEADDocument = DocumentFactory.LoadFromCache(loadFromCacheOptions) 
      ' Make sure it has the same user token 
      Assert.IsNotNull(document) 
      Assert.AreEqual(userToken, document.UserToken) 
   End Using 
 
   ' This should fail 
   loadFromCacheOptions.UserToken = Nothing 
   Console.WriteLine("Deleting the document without a user token") 
   Dim isDeletedFromCache As Boolean = 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) 
End Sub 
 
Public Shared Function GetCache() As ObjectCache 
   ' Create a LEADTOOLS FileCache object 
 
   Dim cacheDir As String = Path.Combine(ImagesPath.Path, "cache") 
   If Directory.Exists(cacheDir) Then 
      Directory.Delete(cacheDir, True) 
   End If 
 
   Directory.CreateDirectory(cacheDir) 
 
   Dim cache As New FileCache() 
   cache.CacheDirectory = cacheDir 
 
   Return cache 
End Function 
Requirements
Target Platforms
Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.