public async Task<AdApplication> UpdateApplicationAsync(AdApplication app)
 {
     return await SendAsync<AdApplication>(_tenantId, Parameters.GRAPH_API_VERSION, $"/applications/{app.ObjectId}", RestSharp.Method.PATCH, app);
 }        
        private async Task RenewServiceCredential(GraphClient cl, ManagementPack configMp, AdApplication app, TimeSpan credValidity)
        {
            if (app == null)
                throw new ArgumentNullException(nameof(app));

            if (app.PasswordCredentials != null)
            {
                var passCred = app.PasswordCredentials.FirstOrDefault(x => x.CustomKeyIdentifier == Convert.ToBase64String(_emg.Id.ToByteArray()));
                if (passCred != null)
                    app.PasswordCredentials.Remove(passCred);
                await cl.UpdateApplicationAsync(app);
            }

            var pass = RandomString();
            var securePass = new System.Security.SecureString();
            foreach (char c in pass)
                securePass.AppendChar(c);

            var endDate = DateTime.UtcNow + credValidity;

            app.PasswordCredentials.Add(new PasswordCredential()
            {
                EndDate = endDate,
                StartDate = DateTime.UtcNow,
                KeyId = Guid.NewGuid(),
                Value = pass,
                CustomKeyIdentifier = Convert.ToBase64String(_emg.Id.ToByteArray())
            });
            await cl.UpdateApplicationAsync(app);

            bool isNew = false;
            var secData = (BasicCredentialSecureData)_emg.Security.GetSecureData(new SecureDataCriteria($"Name = '{Parameters.SECURE_REFERENCE_NAME}'")).FirstOrDefault();

            if (secData == null)
            {
                secData = new BasicCredentialSecureData();
                isNew = true;                
            }

            secData.UserName = app.AppId.ToString();
            secData.Data = securePass;
            secData.Name = Parameters.SECURE_REFERENCE_NAME;

            if (isNew)
                _emg.Security.InsertSecureData(secData);
            secData.Update();

            var secRefOverride = (ManagementPackSecureReferenceOverride)_emg.Overrides.GetOverrides(new ManagementPackOverrideCriteria($"Name = '{Parameters.SECURE_REFERENCE_OVERRIDE_NAME}'")).FirstOrDefault();
            if (secRefOverride == null)
                secRefOverride = new ManagementPackSecureReferenceOverride(configMp, Parameters.SECURE_REFERENCE_OVERRIDE_NAME);

            secRefOverride.DisplayName = Parameters.SECURE_REFERENCE_OVERRIDE_NAME;
            secRefOverride.Context = EntityClass;
            secRefOverride.SecureReference = ConnectorSecureReference;
            secRefOverride.Value = BitConverter.ToString(secData.SecureStorageId, 0, secData.SecureStorageId.Length).Replace("-", "");

            secRefOverride.GetManagementPack().AcceptChanges();

            Settings.CredentialExpirationDate = endDate;
        }