コード例 #1
0
        public byte[] GetKeyFromAppsetting(string name)
        {
            var cfg = _config ?? GetConfig();

            var values = cfg.GetValues(name);

            if (values == null)
            {
                throw new ApplicationException("Could not get session authentication key. The appsetting " + name + " did not exist.");
            }

            if (values.Length > 1)
            {
                throw new ApplicationException("Multiple values found for the appsetting " + name + ". Please configure only one.");
            }

            var validationKey = values[0];

            string failureReason;

            if (!_validator.IsValidKey(validationKey, out failureReason))
            {
                throw new ApplicationException("Invalid session authentication key in appsetting. " + failureReason);
            }

            var hexbinary = SoapHexBinary.Parse(validationKey);
            var key       = hexbinary.Value;

            hexbinary.Value = new byte[0];
            return(key);
        }
        public override void Validate(object value)
        {
            var sessionIDAuthenticationConfig = (SessionIDAuthenticationConfigurationElement)value;

            if (!sessionIDAuthenticationConfig.Enabled || sessionIDAuthenticationConfig.UseMachineKey)
            {
                return;
            }

            var authenticationKey = sessionIDAuthenticationConfig.AuthenticationKey;

            var keyIsConfigured        = !Regex.IsMatch(authenticationKey, "^0+$");
            var appsettingIsConfigured = !String.IsNullOrEmpty(sessionIDAuthenticationConfig.AuthenticationKeyAppsetting);

            //Both configured
            if (keyIsConfigured && appsettingIsConfigured)
            {
                const string message = "Both authenticationKey and authenticationKeyAppsetting are configured. Key must be specified with one or the other.";
                throw new ConfigurationErrorsException(message);
            }

            //None configured
            if (!keyIsConfigured && !appsettingIsConfigured)
            {
                const string message = "An authenticationKey or authenticationKeyAppsetting was not configured. Supply a value, or enable \"useMachineKey\" to use the machineKey validation key instead.";
                throw new ConfigurationErrorsException(message);
            }

            //Only appsetting
            if (appsettingIsConfigured)
            {
                return;
            }

            //Only key, validate it
            string reason;

            if (!_keyValidator.IsValidKey(authenticationKey, out reason))
            {
                throw new ConfigurationErrorsException("Invalid authenticationKey." + reason);
            }
        }