The following code illustrates a way to use the Forefront Identity Manager Certificate Management (FIM CM) Provision API to create new requests on a user’s active Smart Card. Given a User UUID, it first finds all active Smart Cards assigned to that user and then prints out their GUIDs. Then it asks the user to pick a GUID for one of the Smart Cards, and uses that GUID to create Recover, Unblock, and Retire requests for that Smartcard.

Caution:
When using this code, remember to keep in mind the guidelines on whether or not you need to use .NET Remoting. See .NET Framework Remoting in the Provision API for more information.
C#  Copy Code
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Net;

using Microsoft.Clm.Shared;
using Microsoft.Clm.Shared.Certificates;
using Microsoft.Clm.Shared.ProfileTemplates;
using Microsoft.Clm.Shared.Requests;
using Microsoft.Clm.Shared.Smartcards;
using Microsoft.Clm.Provision;

namespace Contoso.Clm.Test
{
	class SmartCardRequestTests
	{
		private static void Output(string prefix, ProfileTemplate profileTemplate)
		{
			string outputLine = string.Format(System.Globalization.CultureInfo.CurrentCulture, 
				"{0}{1} [{2}]", prefix, profileTemplate.CommonName, profileTemplate.Uuid);
			Console.WriteLine(outputLine);
	}

		private static void Output(string prefix, ReadOnlyCollection<Microsoft.Clm.Shared.Smartcards.Smartcard> list)
		{
			foreach (Microsoft.Clm.Shared.Smartcards.Smartcard smartcard in list)
			{
				Output(prefix, smartcard);
		}
	}

		private static void Output(string prefix, Microsoft.Clm.Shared.Smartcards.Smartcard smartcard)
		{
			string outputLine = string.Format(System.Globalization.CultureInfo.CurrentCulture,
				"{0}{1} [{2}]", prefix, smartcard.Uuid, smartcard.AssignedUserUuid);
			Console.WriteLine(outputLine);
	}

		public  void RunRequestTests(string userUuid)
		{
			Console.WriteLine();
			Console.WriteLine();
			Console.WriteLine("[Recover, Unblock, Retire, New Card]");

			string secondLinePrefix = "	";

			/*****/
			Console.WriteLine("Step 1 - Find all active smartcards assigned to a user");
			Guid guid = new Guid(userUuid);
			ReadOnlyCollection<Microsoft.Clm.Shared.Smartcards.Smartcard> allSmartcards = 
				FindOperations.FindSmartcards(new Microsoft.Clm.Shared.Smartcards.SmartcardStatus[] {Microsoft.Clm.Shared.Smartcards.SmartcardStatus.Active},
				new string[] { },
				new string[] { },
				new Guid[] { guid },
				DateTime.MinValue,
				DateTime.MaxValue);

			Output(secondLinePrefix, allSmartcards); 	
			/****/

			/*****/


			List<Microsoft.Clm.Shared.Requests.DataCollectionItem> list = new List<Microsoft.Clm.Shared.Requests.DataCollectionItem>();
			list.Add(new Microsoft.Clm.Shared.Requests.DataCollectionItem("abc", "My Value")); 		Microsoft.Clm.Shared.Requests.DataCollection dc = new Microsoft.Clm.Shared.Requests.DataCollection(list);

			/*****/
			Console.WriteLine("Step 2 - Initiate Unblock smartcard");
			Console.Write("Input Smartcard Uuid for Unblock: ");
			string sUuid = Console.ReadLine();
			if (!string.IsNullOrEmpty(sUuid))
			{
				Guid smartcardUuid = new Guid(sUuid);
				Microsoft.Clm.Shared.Smartcards.Smartcard smartcard = FindOperations.GetSmartcard(smartcardUuid);
				Request unblockRequest = RequestOperations.InitiateUnblock(smartcard, dc, "Unblock comment", 202);
				Output(secondLinePrefix, unblockRequest);
		}
			/****/

			/*****/
			Console.WriteLine("Step 3 - Initiate smartcard replacement");
			Console.Write("Input Smartcard Uuid to Replace: ");
			sUuid = Console.ReadLine();
			if (!string.IsNullOrEmpty(sUuid))
			{
				Guid smartcardUuid = new Guid(sUuid);
				Microsoft.Clm.Shared.Smartcards.Smartcard smartcard = FindOperations.GetSmartcard(smartcardUuid);
				Request recoverRequest = RequestOperations.InitiateRecover(smartcard, dc, "Recover comment", 203);
				Output(secondLinePrefix, recoverRequest);

				/*****/
				Console.WriteLine("Step 4 - Create New Card");
				Microsoft.Clm.Shared.Smartcards.Smartcard sc = RequestOperations.CreateSmartcard("12345678", "axalto", null /*atr*/, recoverRequest);
				Output(secondLinePrefix, sc);

				/****/
		}
			/****/

			/*****/
			Console.WriteLine("Step 5 - Initiate Retire smartcard");
			Console.Write("Input Smartcard Uuid for Retire: ");
			sUuid = Console.ReadLine();
			if (!string.IsNullOrEmpty(sUuid))
			{
				Guid smartcardUuid = new Guid(sUuid);
				Microsoft.Clm.Shared.Smartcards.Smartcard smartcard = FindOperations.GetSmartcard(smartcardUuid);
				Request retireRequest = RequestOperations.InitiateRetire(smartcard, dc, "Retire comment", 204);
				Output(secondLinePrefix, retireRequest);
		}
			/****/
	}
}
}

See Also