/// <summary> /// Ensures the service principal. /// </summary> /// <returns>PSADServicePrincipal.</returns> public PSADServicePrincipal EnsureServicePrincipal() { string applicationId = KailaniAppId.ToString(); var servicePrincipals = ActiveDirectoryClient.FilterServicePrincipals(new ODataQuery <ServicePrincipal>(s => s.AppId == applicationId)); PSADServicePrincipal servicePrincipal = servicePrincipals.FirstOrDefault(); if (servicePrincipal == null) { VerboseLogger.Invoke(StorageSyncResources.CreateServicePrincipalMessage); // Create an application and get the applicationId var passwordCredential = new PSADPasswordCredential() { StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(1), KeyId = Guid.NewGuid(), Password = SecureStringExtensions.ConvertToString(Guid.NewGuid().ToString().ConvertToSecureString()) }; var createParameters = new CreatePSServicePrincipalParameters { ApplicationId = KailaniAppId, AccountEnabled = true, PasswordCredentials = new PSADPasswordCredential[] { passwordCredential } }; servicePrincipal = ActiveDirectoryClient.CreateServicePrincipal(createParameters); } return(servicePrincipal); }
/// <summary> /// Ensures the service principal. /// </summary> /// <returns>PSADServicePrincipal.</returns> public PSADServicePrincipal EnsureServicePrincipal() { string applicationId = CurrentApplicationId.ToString(); string appObjectId = ActiveDirectoryClient.GetServicePrincipalsIdByAppId(CurrentApplicationId); PSADServicePrincipal servicePrincipal = ActiveDirectoryClient.GetServicePrincipalByObjectId(appObjectId); if (servicePrincipal == null) { VerboseLogger.Invoke(StorageSyncResources.CreateServicePrincipalMessage); // Create an application and get the applicationId var passwordCredential = new PSADPasswordCredential() { StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(1), KeyId = Guid.NewGuid(), Password = SecureStringExtensions.ConvertToString(Guid.NewGuid().ToString().ConvertToSecureString()) }; var createParameters = new CreatePSServicePrincipalParameters { ApplicationId = CurrentApplicationId, AccountEnabled = bool.TrueString, PasswordCredentials = new PSADPasswordCredential[] { passwordCredential } }; servicePrincipal = ActiveDirectoryClient.CreateServicePrincipal(createParameters); } return(servicePrincipal); }