/// <summary> /// Add a service key. /// </summary> private static void AddServiceKey(string displayName, byte[] keyValue, string protectionPassword, ServiceKeyType keyType, ServiceKeyUsage keyUsage) { ManagementService svc = ManagementServiceHelper.CreateManagementServiceClient(); UTF8Encoding enc = new UTF8Encoding(); ServiceKey serviceKey = new ServiceKey() { DisplayName = displayName, Type = keyType.ToString(), Usage = keyUsage.ToString(), Value = keyValue, Password = string.IsNullOrEmpty(protectionPassword) ? null : enc.GetBytes(protectionPassword), StartDate = defaultStartTime, EndDate = defaultEndTime }; svc.AddToServiceKeys(serviceKey); svc.SaveChangesBatch(); }
/// <summary> /// Update the service key value and the protection password. /// </summary> private static void UpdateServiceKey(string displayName, byte[] keyValue, string protectionPassword, ServiceKeyType keyType) { UTF8Encoding enc = new UTF8Encoding(); ManagementService svc = ManagementServiceHelper.CreateManagementServiceClient(); ServiceKey serviceKey = svc.ServiceKeys.Where(sk => sk.DisplayName == displayName && sk.Type == keyType.ToString()).First(); serviceKey.Value = keyValue; serviceKey.Password = string.IsNullOrEmpty(protectionPassword) ? null : enc.GetBytes(protectionPassword); svc.UpdateObject(serviceKey); svc.SaveChangesBatch(); }