Пример #1
0
        public static void AddIdentity(this ManagementService svc,
                                       Credentials serviceIdentity, DateTime startDate, DateTime endDate)
        {
            Contract.Requires(svc != null);
            Contract.Requires(serviceIdentity != null);
            Contract.Requires(startDate != default(DateTime));
            Contract.Requires(endDate > startDate);

            var sid = new ServiceIdentity()
            {
                Name = serviceIdentity.UserName
            };

            var key = new ServiceIdentityKey()
            {
                StartDate   = startDate,
                EndDate     = endDate,
                Type        = "Password",
                Usage       = "Password",
                Value       = Encoding.UTF8.GetBytes(serviceIdentity.Password),
                DisplayName = string.Format(CultureInfo.InvariantCulture,
                                            "{0} key for {1}", "Password", serviceIdentity.UserName)
            };

            svc.AddToServiceIdentities(sid);

            svc.AddRelatedObject(sid, "ServiceIdentityKeys", key);

            svc.SaveChanges(SaveChangesOptions.Batch);
        }
        /// <summary>
        /// Creates  a new ServiceIdentity and an associated key of the value, type, and usage specified.
        /// </summary>
        public static ServiceIdentity CreateServiceIdentity(this ManagementService svc, string name, byte[] keyValue, ServiceIdentityKeyType keyType, ServiceIdentityKeyUsage keyUsage)
        {
            ServiceIdentity sid = new ServiceIdentity()
            {
                Name = name
            };

            DateTime startDate, endDate;

            if (keyType == ServiceIdentityKeyType.X509Certificate)
            {
                //
                // ACS requires that the key start and end dates be within the certificate's validity period.
                //
                X509Certificate2 cert = new X509Certificate2(keyValue);

                startDate = cert.NotBefore.ToUniversalTime();
                endDate   = cert.NotAfter.ToUniversalTime();
            }
            else
            {
                startDate = DateTime.UtcNow;
                endDate   = DateTime.MaxValue;
            }

            ServiceIdentityKey key = new ServiceIdentityKey()
            {
                EndDate     = endDate.ToUniversalTime(),
                StartDate   = startDate.ToUniversalTime(),
                Type        = keyType.ToString(),
                Usage       = keyUsage.ToString(),
                Value       = keyValue,
                DisplayName = String.Format(CultureInfo.InvariantCulture, "{0} key for {1}", keyType.ToString(), name)
            };

            svc.AddToServiceIdentities(sid);
            svc.AddRelatedObject(
                sid,
                "ServiceIdentityKeys",
                key);

            return(sid);
        }