我试图在找到对象后更新属性。尝试过不同的案例。
我的创建代码:
using (var pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.SingleThreaded))
{
var slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
using (var session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, "1111");
var objectAttributes = new List<ObjectAttribute>
{
new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA),
new ObjectAttribute(CKA.CKA_TOKEN, true),
new ObjectAttribute(CKA.CKA_MODIFIABLE, true),
new ObjectAttribute(CKA.CKA_APPLICATION, txtTypeofData.Text),
new ObjectAttribute(CKA.CKA_LABEL,txtMsisdn.Text),
new ObjectAttribute(CKA.CKA_VALUE, "Data object content original " + DateTime.Now)
};
var result = session.CreateObject(objectAttributes);
session.Logout();
}
}
我的修改代码是:
using (Pkcs11 pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.MultiThreaded))
{
var slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
using (Session session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, "1111");
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_APPLICATION, txtTypeofData.Text));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, txtMsisdn.Text));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_MODIFIABLE, true));
var findA = session.FindAllObjects(objectAttributes);
if(findA != null && findA.Count > 0)
{
List<ObjectAttribute> objectAttributesNew = new List<ObjectAttribute>();
objectAttributesNew.Add(new ObjectAttribute(CKA.CKA_VALUE, "Data object content two changed " + DateTime.Now));
session.SetAttributeValue(findA[0], objectAttributesNew);
}
session.Logout();
}
}