示例#1
0
        public SessionCredentials(SdkConfig config, SenderCredentials senderCreds)
        {
            if (String.IsNullOrWhiteSpace(config.SessionId))
            {
                throw new ArgumentException("Required SessionId not supplied in params");
            }

            SessionId = config.SessionId;
            if (!String.IsNullOrWhiteSpace(config.EndpointUrl))
            {
                Endpoint = new Endpoint(config);
            }
            else
            {
                Endpoint = senderCreds.Endpoint;
            }

            SenderCreds           = senderCreds;
            CurrentCompanyId      = config.CurrentCompanyId;
            CurrentUserId         = config.CurrentUserId;
            CurrentUserIsExternal = config.CurrentUserIsExternal;
            MockHandler           = config.MockHandler;

            Logger           = config.Logger;
            LogMessageFormat = config.LogFormatter;
            LogLevel         = config.LogLevel;
        }
        /// <summary>
        /// Constructs an SdkConfig based on the params
        /// </summary>
        /// <param name="senderCreds"></param>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        private SdkConfig getConfig(SenderCredentials senderCreds, Endpoint endpoint)
        {
            SdkConfig config = new SdkConfig()
            {
                SenderId                = senderCreds.SenderId,
                SenderPassword          = senderCreds.Password,
                ControlId               = "sessionProvider",
                UniqueId                = false,
                DtdVersion              = "3.0",
                Transaction             = false,
                EndpointUrl             = endpoint.Url,
                NoRetryServerErrorCodes = null // Retry all 500 level errors
            };

            return(config);
        }
        /// <summary>
        /// Gets a new SessionCredentials object based on the params
        /// </summary>
        /// <param name="sessionCreds"></param>
        /// <returns></returns>
        public async Task <SessionCredentials> FromSessionCredentials(SessionCredentials sessionCreds)
        {
            SenderCredentials senderCreds = sessionCreds.SenderCreds;
            Endpoint          endpoint    = sessionCreds.Endpoint;

            SdkConfig config = getConfig(senderCreds, endpoint);

            config.SessionId    = sessionCreds.SessionId;
            config.MockHandler  = sessionCreds.MockHandler;
            config.Logger       = sessionCreds.Logger;
            config.LogFormatter = sessionCreds.LogMessageFormat;
            config.LogLevel     = sessionCreds.LogLevel;

            SdkConfig session = await getAPISession(config);

            return(new SessionCredentials(session, senderCreds));
        }
        /// <summary>
        /// Gets a new SessionCredentials object based on the params
        /// </summary>
        /// <param name="loginCreds"></param>
        /// <returns></returns>
        public async Task <SessionCredentials> FromLoginCredentials(LoginCredentials loginCreds)
        {
            SenderCredentials senderCreds = loginCreds.SenderCreds;
            Endpoint          endpoint    = loginCreds.Endpoint;

            SdkConfig config = getConfig(senderCreds, endpoint);

            config.CompanyId    = loginCreds.CompanyId;
            config.UserId       = loginCreds.UserId;
            config.UserPassword = loginCreds.Password;
            config.MockHandler  = loginCreds.MockHandler;
            config.Logger       = loginCreds.Logger;
            config.LogFormatter = loginCreds.LogMessageFormat;
            config.LogLevel     = loginCreds.LogLevel;

            SdkConfig session = await getAPISession(config);

            return(new SessionCredentials(session, senderCreds));
        }
        public LoginCredentials(SdkConfig config, SenderCredentials senderCreds)
        {
            string envProfileName = Environment.GetEnvironmentVariable(CompanyProfileEnvName);

            if (String.IsNullOrWhiteSpace(envProfileName))
            {
                envProfileName = DefaultCompanyProfile;
            }
            if (String.IsNullOrWhiteSpace(config.ProfileName))
            {
                config.ProfileName = envProfileName;
            }
            if (String.IsNullOrWhiteSpace(config.CompanyId))
            {
                config.CompanyId = Environment.GetEnvironmentVariable(CompanyIdEnvName);
            }
            if (String.IsNullOrWhiteSpace(config.UserId))
            {
                config.UserId = Environment.GetEnvironmentVariable(UserIdEnvName);
            }
            if (String.IsNullOrWhiteSpace(config.UserPassword))
            {
                config.UserPassword = Environment.GetEnvironmentVariable(UserPasswordEnvName);
            }

            if (
                String.IsNullOrWhiteSpace(config.CompanyId) &&
                String.IsNullOrWhiteSpace(config.UserId) &&
                String.IsNullOrWhiteSpace(config.UserPassword) &&
                !String.IsNullOrWhiteSpace(config.ProfileName)
                )
            {
                ProfileCredentialProvider profileProvider = new ProfileCredentialProvider();
                SdkConfig profileCreds = profileProvider.GetLoginCredentials(config);
                config.CompanyId    = !String.IsNullOrWhiteSpace(profileCreds.CompanyId) ? profileCreds.CompanyId : config.CompanyId;
                config.UserId       = !String.IsNullOrWhiteSpace(profileCreds.UserId) ? profileCreds.UserId : config.UserId;
                config.UserPassword = !String.IsNullOrWhiteSpace(profileCreds.UserPassword) ? profileCreds.UserPassword : config.UserPassword;
            }

            if (String.IsNullOrWhiteSpace(config.CompanyId))
            {
                throw new ArgumentException("Required CompanyId not supplied in params or env variable \"" + CompanyIdEnvName + "\"");
            }
            if (String.IsNullOrWhiteSpace(config.UserId))
            {
                throw new ArgumentException("Required UserId not supplied in params or env variable \"" + UserIdEnvName + "\"");
            }
            if (String.IsNullOrWhiteSpace(config.UserPassword))
            {
                throw new ArgumentException("Required UserPassword not supplied in params or env variable \"" + UserPasswordEnvName + "\"");
            }

            CompanyId   = config.CompanyId;
            UserId      = config.UserId;
            Password    = config.UserPassword;
            SenderCreds = senderCreds;
            MockHandler = config.MockHandler;

            Logger           = config.Logger;
            LogMessageFormat = config.LogFormatter;
            LogLevel         = config.LogLevel;
        }