internal static ADUser FetchRecipientFromMailboxId(string domainController, MailboxIdParameter mailbox, out IRecipientSession session, OrganizationId orgId)
		{
			session = DirectorySessionFactory.Default.GetTenantOrRootOrgRecipientSession(domainController, true, ConsistencyMode.IgnoreInvalid, orgId.ToADSessionSettings(), 191, "FetchRecipientFromMailboxId", "f:\\15.00.1497\\sources\\dev\\Management\\src\\Management\\Elc\\StoreRetentionPolicyTagHelper.cs");
			LocalizedString? localizedString = null;
			IEnumerable<ADUser> objects = mailbox.GetObjects<ADUser>(null, session, null, out localizedString);
			ADUser aduser = null;
			if (objects != null)
			{
				foreach (ADUser aduser2 in objects)
				{
					if (aduser != null)
					{
						throw new ManagementObjectAmbiguousException(Strings.ErrorRecipientNotUnique(mailbox.ToString()));
					}
					aduser = aduser2;
				}
			}
			if (aduser == null)
			{
				throw new ManagementObjectNotFoundException(localizedString ?? Strings.ErrorObjectNotFound(mailbox.ToString()));
			}
			return aduser;
		}
Exemple #2
0
        internal static ExchangePrincipal GetExchangePrincipal(ADSessionSettings sessionSettings, IRecipientSession recipientSession, MailboxIdParameter mailbox, string cmdletName, out Exception localizedError, out ADUser adUser)
        {
            localizedError = null;
            adUser         = null;
            mailbox.SearchWithDisplayName = false;
            IEnumerable <ADRecipient> objects = mailbox.GetObjects <ADRecipient>(null, recipientSession);
            ExchangePrincipal         result;

            using (IEnumerator <ADRecipient> enumerator = objects.GetEnumerator())
            {
                if (!enumerator.MoveNext())
                {
                    localizedError = new RecipientNotFoundException(mailbox.ToString());
                    result         = null;
                }
                else
                {
                    ADUser aduser = enumerator.Current as ADUser;
                    if (enumerator.MoveNext())
                    {
                        localizedError = new RecipientNotUniqueException(mailbox.ToString());
                        result         = null;
                    }
                    else if (aduser == null || (aduser.RecipientType != RecipientType.UserMailbox && aduser.RecipientType != RecipientType.MailUser))
                    {
                        localizedError = new RecipientNotValidException(mailbox.ToString());
                        result         = null;
                    }
                    else if ((int)aduser.ExchangeVersion.ExchangeBuild.Major > Server.CurrentExchangeMajorVersion || (int)aduser.ExchangeVersion.ExchangeBuild.Major < Server.Exchange2009MajorVersion)
                    {
                        localizedError = new ServerVersionNotSupportedException(cmdletName, Server.CurrentExchangeMajorVersion, (int)aduser.ExchangeVersion.ExchangeBuild.Major);
                        result         = null;
                    }
                    else
                    {
                        ADSessionSettings adSettings = sessionSettings;
                        IRecipientSession tenantOrRootOrgRecipientSession = DirectorySessionFactory.Default.GetTenantOrRootOrgRecipientSession(ConsistencyMode.FullyConsistent, sessionSettings, 125, "GetExchangePrincipal", "f:\\15.00.1497\\sources\\dev\\Management\\src\\Management\\AirSync\\MobileDeviceTaskHelper.cs");
                        if (TaskHelper.ShouldUnderscopeDataSessionToOrganization(tenantOrRootOrgRecipientSession, aduser))
                        {
                            IDirectorySession directorySession = TaskHelper.UnderscopeSessionToOrganization(tenantOrRootOrgRecipientSession, aduser.OrganizationId, sessionSettings, true);
                            adSettings = directorySession.SessionSettings;
                        }
                        ExchangePrincipal exchangePrincipal = null;
                        try
                        {
                            adUser            = aduser;
                            exchangePrincipal = ExchangePrincipal.FromADUser(adSettings, aduser, RemotingOptions.AllowCrossSite);
                        }
                        catch (UserHasNoMailboxException ex)
                        {
                            localizedError = ex;
                            return(null);
                        }
                        if (exchangePrincipal == null)
                        {
                            localizedError = new ExchangePrincipalNotFoundException(aduser.ToString());
                            result         = null;
                        }
                        else
                        {
                            result = exchangePrincipal;
                        }
                    }
                }
            }
            return(result);
        }
        internal static NetworkCredential GetDefaultTestAccount(TestLiveIdAuthenticationTask.ClientAccessContext context, LiveIdAuthenticationUserTypeEnum userType)
        {
            SmtpAddress?defaultTestUser = null;

            if (!TestConnectivityCredentialsManager.IsExchangeMultiTenant())
            {
                throw new InvalidOperationException();
            }
            if (userType <= LiveIdAuthenticationUserTypeEnum.ManagedBusiness)
            {
                if (userType != LiveIdAuthenticationUserTypeEnum.ManagedConsumer)
                {
                    if (userType == LiveIdAuthenticationUserTypeEnum.ManagedBusiness)
                    {
                        defaultTestUser = TestConnectivityCredentialsManager.GetMultiTenantAutomatedTaskUser(context.Instance, context.ConfigurationSession, context.Site, DatacenterUserType.BPOS);
                    }
                }
                else
                {
                    defaultTestUser = TestConnectivityCredentialsManager.GetMultiTenantAutomatedTaskUser(context.Instance, context.ConfigurationSession, context.Site, DatacenterUserType.EDU);
                }
            }
            else if (userType == LiveIdAuthenticationUserTypeEnum.FederatedConsumer || userType == LiveIdAuthenticationUserTypeEnum.FederatedBusiness)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(), null);
            }
            if (defaultTestUser == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(), null);
            }
            MailboxIdParameter localMailboxId = new MailboxIdParameter(string.Format("{0}", defaultTestUser.Value.Local));
            ADUser             aduser         = TestLiveIdAuthenticationTask.EnsureSingleObject <ADUser>(() => localMailboxId.GetObjects <ADUser>(null, TestLiveIdAuthenticationTask.GetRecipientSession(defaultTestUser.GetValueOrDefault().Domain)));

            if (aduser == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(defaultTestUser.ToString()), null);
            }
            ExchangePrincipal exchangePrincipal = ExchangePrincipal.FromADUser(aduser.OrganizationId.ToADSessionSettings(), aduser);

            if (exchangePrincipal == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(defaultTestUser.ToString()), null);
            }
            NetworkCredential  networkCredential  = new NetworkCredential(defaultTestUser.Value.ToString(), string.Empty, context.WindowsDomain);
            NetworkCredential  networkCredential2 = TestLiveIdAuthenticationTask.MakeCasCredential(networkCredential);
            LocalizedException ex = TestConnectivityCredentialsManager.LoadAutomatedTestCasConnectivityInfo(exchangePrincipal, networkCredential2);

            if (ex != null)
            {
                throw ex;
            }
            networkCredential.Domain   = defaultTestUser.Value.Domain;
            networkCredential.Password = networkCredential2.Password;
            return(networkCredential);
        }
        internal static UserWithCredential GetDefaultTestAccount(CommonTestTasks.ClientAccessContext context)
        {
            SmtpAddress?smtpAddress;

            if (TestConnectivityCredentialsManager.IsExchangeMultiTenant())
            {
                smtpAddress = TestConnectivityCredentialsManager.GetMultiTenantAutomatedTaskUser(context.Instance, context.ConfigurationSession, context.Site);
            }
            else
            {
                smtpAddress = TestConnectivityCredentialsManager.GetEnterpriseAutomatedTaskUser(context.Site, context.WindowsDomain);
            }
            if (smtpAddress == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(), null);
            }
            MailboxIdParameter localMailboxId = new MailboxIdParameter(string.Format("{0}\\{1}", smtpAddress.Value.Domain, smtpAddress.Value.Local));
            ADUser             aduser         = CommonTestTasks.EnsureSingleObject <ADUser>(() => localMailboxId.GetObjects <ADUser>(null, context.RecipientSession));

            if (aduser == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(smtpAddress.ToString()), null);
            }
            ExchangePrincipal exchangePrincipal = ExchangePrincipal.FromADUser(aduser, null);

            if (exchangePrincipal == null)
            {
                throw new MailboxNotFoundException(new MailboxIdParameter(smtpAddress.ToString()), null);
            }
            NetworkCredential networkCredential  = new NetworkCredential(smtpAddress.Value.ToString(), string.Empty, context.WindowsDomain);
            NetworkCredential networkCredential2 = CommonTestTasks.MakeCasCredential(networkCredential);
            bool flag = false;
            LocalizedException ex;

            if (Datacenter.IsLiveIDForExchangeLogin(true) || context.MonitoringContext)
            {
                ex = TestConnectivityCredentialsManager.LoadAutomatedTestCasConnectivityInfo(exchangePrincipal, networkCredential2);
            }
            else
            {
                ex = TestConnectivityCredentialsManager.ResetAutomatedCredentialsAndVerify(exchangePrincipal, networkCredential2, false, out flag);
            }
            if (ex != null)
            {
                throw ex;
            }
            networkCredential.Domain   = smtpAddress.Value.Domain;
            networkCredential.Password = networkCredential2.Password;
            return(new UserWithCredential
            {
                User = aduser,
                Credential = networkCredential
            });
        }