예제 #1
0
 public KeyCredential CreateKeyCredential()
 {
     X509Certificate applicationCertificate = new X509Certificate("SampleApplicationCredential.cer");
     KeyCredential cred = new KeyCredential();
     cred.StartDate = DateTime.Now;
     cred.EndDate = DateTime.Now.AddMonths(12);
     cred.KeyId = Guid.NewGuid();
     cred.Value = Convert.ToBase64String(applicationCertificate.GetRawCertData());
     cred.Type = "AsymmetricX509Cert";
     cred.Usage = "Verify";
     return cred;
 }
예제 #2
0
        public Application CreateApplication(PasswordCredential passwordCredential = null, KeyCredential keyCredential = null, string applicationName = null)
        {
            var appName = applicationName ?? TestUtilities.GenerateName("adApplication");
            var url = string.Format("http://{0}/home", appName);
            var parameters = new ApplicationCreateParameters();

            parameters.AvailableToOtherTenants = false;
            parameters.DisplayName = appName;
            parameters.Homepage = url;
            parameters.IdentifierUris = new[] { url };
            parameters.ReplyUrls = new[] { url };

            if (passwordCredential != null)
            {
                parameters.PasswordCredentials = new PasswordCredential[] { passwordCredential };
            }

            if (keyCredential != null)
            {
                parameters.KeyCredentials = new KeyCredential[] { keyCredential };
            }

            return GraphClient.Application.Create(parameters).Application;
        }
 private void ValidateKeyCredential(KeyCredential credential)
 {
     if (credential == null || string.IsNullOrEmpty(credential.KeyId) || string.IsNullOrEmpty(credential.Value) ||
         string.IsNullOrEmpty(credential.Type) || string.IsNullOrEmpty(credential.Usage) || credential.StartDate == null || credential.EndDate == null)
     {
         throw new InvalidOperationException(ProjectResources.KeyCredentialNotValid);
     }
 }
        public PSADCredential CreateSpKeyCredential(string spObjectId, KeyCredential credential)
        {
            ValidateKeyCredential(credential);

            var keyCredsList = GetSpKeyCredentials(spObjectId);

            // Add new KeyCredential to existing KeyCredential list
            keyCredsList.Add(credential);

            PatchSpKeyCredentials(spObjectId, keyCredsList);

            return credential.ToPSADCredential();
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (!string.IsNullOrEmpty(ServicePrincipalName))
                {
                    ObjectId = ActiveDirectoryClient.GetObjectIdFromSPN(ServicePrincipalName);
                }

                if (!string.IsNullOrEmpty(Password))
                {
                    // Create object for password credential
                    var passwordCredential = new PasswordCredential()
                    {
                        EndDate = EndDate,
                        StartDate = StartDate,
                        KeyId = Guid.NewGuid().ToString(),
                        Value = Password
                    };
                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new password to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpPasswordCredential(ObjectId, passwordCredential));
                    }
                }
                else if (!string.IsNullOrEmpty(CertValue))
                {
                    // Create object for key credential
                    var keyCredential = new KeyCredential()
                    {
                        EndDate = EndDate,
                        StartDate = StartDate,
                        KeyId = Guid.NewGuid().ToString(),
                        Value = CertValue,
                        Type = "AsymmetricX509Cert",
                        Usage = "Verify"
                    };

                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new caertificate to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpKeyCredential(ObjectId, keyCredential));
                    }
                }
                else
                {
                    throw new InvalidOperationException("No valid keyCredential or passwordCredential to update!!");
                }
            });
        }