I <span data-scayt_word="didnt" data-scaytid="1">didnt copy that, we create session at application begins and use this sessions with code in up.
public <span data-scayt_word="BlockingCollection" data-scaytid="12">BlockingCollection<Session> Sessions { get; set; }
Cryptoki.Licensee = SecurityConfiguration.NcryptokiLicensee;
Cryptoki.ProductKey = SecurityConfiguration.NcryptokiProductKey;
// <span data-scayt_word="TODO" data-scaytid="17">TODO : Get Active Slot Description From Singleton dapper repository. (<span data-scayt_word="HsmKeyInformation" data-scaytid="18">HsmKeyInformation)
<span data-scayt_word="Cryptoki" data-scaytid="21">Cryptoki <span data-scayt_word="cryptoki" data-scaytid="23">cryptoki = new <span data-scayt_word="Cryptoki" data-scaytid="22">Cryptoki("cryptoki.dll");
cryptoki.Initialize(true);
<span data-scayt_word="CryptokiInfo" data-scaytid="25">CryptokiInfo info = cryptoki.Info;
// Reads the set of slots containing a token
<span data-scayt_word="SlotList" data-scaytid="27">SlotList slots = cryptoki.Slots;
if (slots.Count == 0)
{
throw new Exception("No slot available");
}
<span data-scayt_word="int" data-scaytid="29">int <span data-scayt_word="slotId" data-scaytid="30">slotId = int.Parse(this.GetProtectedConfigValue("<span data-scayt_word="SlotId" data-scaytid="32">SlotId"));
<span data-scayt_word="var" data-scaytid="33">var slot = slots.SingleOrDefault(x => x.SlotID == <span data-scayt_word="slotId" data-scaytid="31">slotId);
if (slot == null)
slot = slots[0];
if (!slot.IsTokenPresent)
{
throw new Exception("No token inserted in the slot: " + slot.Info.Description);
}
<span data-scayt_word="var" data-scaytid="35">var token = slot.Token;
string <span data-scayt_word="slotPassword" data-scaytid="37">slotPassword = this.GetProtectedConfigValue("<span data-scayt_word="SlotPassword" data-scaytid="38">SlotPassword");
for (<span data-scayt_word="int" data-scaytid="39">int i = 0; i < 10; i++)
{
Session <span data-scayt_word="readonlySession" data-scaytid="41">readonlySession = token.OpenSession(Session.CKF_SERIAL_SESSION | Session.CKF_RW_SESSION,
null,
null);
//this.currentSession = <span data-scayt_word="readonlySession" data-scaytid="42">readonlySession;
<span data-scayt_word="int" data-scaytid="43">int <span data-scayt_word="nRes" data-scaytid="45">nRes = 0;
if (!readonlySession.IsLoggedIn)
{
<span data-scayt_word="nRes" data-scaytid="48">nRes = readonlySession.Login(Session.CKU_USER, <span data-scayt_word="slotPassword" data-scaytid="50">slotPassword);
if (<span data-scayt_word="nRes" data-scaytid="49">nRes != 0)
{
throw new Exception("Wrong PIN" + nRes.ToString());
}
Console.WriteLine("<span data-scayt_word="Sesssion" data-scaytid="54">Sesssion login success.");
}
else
{
Console.WriteLine("<span data-scayt_word="Sesssion" data-scaytid="55">Sesssion already logged in.");
}
this.Sessions.Add(<span data-scayt_word="readonlySession" data-scaytid="58">readonlySession);
}