/// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <param name="clientType">The <see cref="ClientType"/> to specify the business or consumer service.</param>
        /// <returns>The <see cref="ServiceInfo"/> for the current session.</returns>
        public virtual Task <ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType)
        {
            if (clientType == ClientType.Consumer)
            {
                var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
                {
                    AppId               = appConfig.MicrosoftAccountAppId,
                    ClientSecret        = appConfig.MicrosoftAccountClientSecret,
                    CredentialCache     = credentialCache,
                    HttpProvider        = httpProvider,
                    ReturnUrl           = appConfig.MicrosoftAccountReturnUrl,
                    Scopes              = appConfig.MicrosoftAccountScopes,
                    WebAuthenticationUi = this.webAuthenticationUi,
                };

                microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
                return(Task.FromResult <ServiceInfo>(microsoftAccountServiceInfo));
            }

            var activeDirectoryServiceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = appConfig.ActiveDirectoryAppId,
                AuthenticationProvider = this.AuthenticationProvider,
                ClientSecret           = appConfig.ActiveDirectoryClientSecret,
                CredentialCache        = credentialCache,
                HttpProvider           = httpProvider,
                ReturnUrl = appConfig.ActiveDirectoryReturnUrl,
            };

            return(Task.FromResult <ServiceInfo>(activeDirectoryServiceInfo));
        }
        public virtual void Setup()
        {
            this.credentialCache = new MockAdalCredentialCache();
            this.httpResponseMessage = new HttpResponseMessage();
            this.serializer = new Serializer();
            this.httpProvider = new MockHttpProvider(this.httpResponseMessage, this.serializer);

            this.serviceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = "12345",
                AuthenticationServiceUrl = "https://login.live.com/authenticate",
                CredentialCache = this.credentialCache.Object,
                HttpProvider = this.httpProvider.Object,
                ReturnUrl = "https://login.live.com/return",
                SignOutUrl = "https://login.live.com/signout",
                TokenServiceUrl = "https://login.live.com/token"
            };
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <param name="clientType">The <see cref="ClientType"/> to specify the business or consumer service.</param>
        /// <returns>The <see cref="ServiceInfo"/> for the current session.</returns>
        public virtual Task <ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType)
        {
            if (clientType == ClientType.Consumer)
            {
                var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
                {
                    AppId               = appConfig.MicrosoftAccountAppId,
                    ClientSecret        = appConfig.MicrosoftAccountClientSecret,
                    CredentialCache     = credentialCache,
                    HttpProvider        = httpProvider,
                    ReturnUrl           = appConfig.MicrosoftAccountReturnUrl,
                    Scopes              = appConfig.MicrosoftAccountScopes,
                    UserId              = this.UserSignInName,
                    WebAuthenticationUi = this.webAuthenticationUi,
                };

                microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider ?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
                return(Task.FromResult <ServiceInfo>(microsoftAccountServiceInfo));
            }

            var activeDirectoryServiceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = appConfig.ActiveDirectoryAppId,
                AuthenticationProvider   = this.AuthenticationProvider,
                AuthenticationServiceUrl = string.IsNullOrEmpty(appConfig.ActiveDirectoryAuthenticationServiceUrl)
                    ? Constants.Authentication.ActiveDirectoryAuthenticationServiceUrl
                    : appConfig.ActiveDirectoryAuthenticationServiceUrl,
                BaseUrl         = appConfig.ActiveDirectoryServiceEndpointUrl,
                ClientSecret    = appConfig.ActiveDirectoryClientSecret,
                CredentialCache = credentialCache,
                HttpProvider    = httpProvider,
                ReturnUrl       = appConfig.ActiveDirectoryReturnUrl,
                ServiceResource = appConfig.ActiveDirectoryServiceResource,
                UserId          = this.UserSignInName,
            };

            return(Task.FromResult <ServiceInfo>(activeDirectoryServiceInfo));
        }
        /// <summary>
        /// Generates the <see cref="ServiceInfo"/> for the current application configuration.
        /// </summary>
        /// <param name="appConfig">The <see cref="AppConfig"/> for the current application.</param>
        /// <param name="credentialCache">The cache instance for storing user credentials.</param>
        /// <param name="httpProvider">The <see cref="IHttpProvider"/> for sending HTTP requests.</param>
        /// <param name="clientType">The <see cref="ClientType"/> to specify the business or consumer service.</param>
        /// <returns>The <see cref="ServiceInfo"/> for the current session.</returns>
        public virtual Task<ServiceInfo> GetServiceInfo(
            AppConfig appConfig,
            CredentialCache credentialCache,
            IHttpProvider httpProvider,
            ClientType clientType)
        {
            if (clientType == ClientType.Consumer)
            {
                var microsoftAccountServiceInfo = new MicrosoftAccountServiceInfo
                {
                    AppId = appConfig.MicrosoftAccountAppId,
                    ClientSecret = appConfig.MicrosoftAccountClientSecret,
                    CredentialCache = credentialCache,
                    HttpProvider = httpProvider,
                    ReturnUrl = appConfig.MicrosoftAccountReturnUrl,
                    Scopes = appConfig.MicrosoftAccountScopes,
                    UserId = this.UserSignInName,
                    WebAuthenticationUi = this.webAuthenticationUi,
                };

                microsoftAccountServiceInfo.AuthenticationProvider = this.AuthenticationProvider?? new MicrosoftAccountAuthenticationProvider(microsoftAccountServiceInfo);
                return Task.FromResult<ServiceInfo>(microsoftAccountServiceInfo);
            }

            var activeDirectoryServiceInfo = new ActiveDirectoryServiceInfo
            {
                AppId = appConfig.ActiveDirectoryAppId,
                AuthenticationProvider = this.AuthenticationProvider,
                ClientSecret = appConfig.ActiveDirectoryClientSecret,
                CredentialCache = credentialCache,
                HttpProvider = httpProvider,
                ReturnUrl = appConfig.ActiveDirectoryReturnUrl,
                UserId = this.UserSignInName,
            };
            
            return Task.FromResult<ServiceInfo>(activeDirectoryServiceInfo);
        }