Beispiel #1
0
        protected override void Execute(NativeActivityContext context)
        {
            var twilio      = context.GetExtension <ITwilioContext>();
            var timeout     = Timeout.Get(context);
            var finishOnKey = FinishOnKey.Get(context);
            var maxLength   = MaxLength.Get(context);
            var transcribe  = Transcribe.Get(context);
            var playBeep    = PlayBeep.Get(context);

            var actionUrl = twilio.ResolveBookmarkUrl(context.CreateTwilioBookmark(OnAction));

            // append record element
            var element = new XElement("Record",
                                       new XAttribute("action", actionUrl),
                                       timeout != null ? new XAttribute("timeout", ((TimeSpan)timeout).TotalSeconds) : null,
                                       finishOnKey != null ? new XAttribute("finishOnKey", finishOnKey) : null,
                                       maxLength != null ? new XAttribute("maxLength", ((TimeSpan)maxLength).TotalSeconds) : null,
                                       transcribe != null ? new XAttribute("transcribe", (bool)transcribe ? "true" : "false") : null,
                                       playBeep != null ? new XAttribute("playBeep", (bool)playBeep ? "true" : "false") : null);

            // write dial element and catch redirect
            GetElement(context).Add(
                element,
                new XElement("Redirect", actionUrl));
        }
Beispiel #2
0
        protected override void Execute(CodeActivityContext executionContext)
        {
            var text   = Text.Get <string>(executionContext);
            var min    = MinLength.Get <int>(executionContext);
            var max    = MaxLength.Get <int>(executionContext);
            var result = Codify(text, min, max);

            Result.Set(executionContext, result);
        }
Beispiel #3
0
        protected override void Execute(CodeActivityContext context)
        {
            var minLength                    = MinLength.Get(context);
            var maxLength                    = MaxLength.Get(context);
            var requiredDigits               = RequiredDigits.Get(context);
            var requiredLowerCaseLetters     = RequiredLowerCaseLetters.Get(context);
            var requiredUpperCaseLetters     = RequiredUpperCaseLetters.Get(context);
            var requiredNonAlphaNumericChars = RequiredSpecialChars.Get(context);

            SpecialChars = string.IsNullOrEmpty(AllowedSpecialChars.Get(context)) ? SpecialChars : AllowedSpecialChars.Get(context);

            var allAvailableChars = LowerCaseLetters + UpperCaseLetters + Digits + SpecialChars;

            if (new[] { minLength, maxLength, requiredDigits,
                        requiredLowerCaseLetters,
                        requiredUpperCaseLetters,
                        requiredNonAlphaNumericChars }.Any(item => item > CapSize))
            {
                throw new ArgumentException("All character requirements have to be lower or equal to " + CapSize);
            }

            if (minLength > maxLength)
            {
                throw new ArgumentException("Minimum password length cannot be greater than maximum password length");
            }

            if (maxLength < requiredDigits + requiredLowerCaseLetters + requiredUpperCaseLetters + requiredNonAlphaNumericChars)
            {
                throw new ArgumentException("Maximum length is lower than total required length");
            }

            var randomBytes = new Byte[4];
            var rngCrypto   = new RNGCryptoServiceProvider();

            rngCrypto.GetBytes(randomBytes);

            var randomGenerator = new Random(BitConverter.ToInt32(randomBytes, 0));

            var passwordLength = randomGenerator.Next(minLength, maxLength + 1);
            var selectedChars  = new List <Char>();

            for (int i = 0; i < requiredDigits; i++)
            {
                selectedChars.Add(Digits.ToCharArray()[randomGenerator.Next(0, Digits.Length)]);
            }

            for (int i = 0; i < requiredLowerCaseLetters; i++)
            {
                selectedChars.Add(LowerCaseLetters.ToCharArray()[randomGenerator.Next(0, LowerCaseLetters.Length)]);
            }

            for (int i = 0; i < requiredUpperCaseLetters; i++)
            {
                selectedChars.Add(UpperCaseLetters.ToCharArray()[randomGenerator.Next(0, UpperCaseLetters.Length)]);
            }

            for (int i = 0; i < requiredNonAlphaNumericChars; i++)
            {
                selectedChars.Add(SpecialChars.ToCharArray()[randomGenerator.Next(0, SpecialChars.Length)]);
            }

            var remainingChars = passwordLength - selectedChars.Count;

            for (int i = 0; i < remainingChars; i++)
            {
                selectedChars.Add(allAvailableChars.ToCharArray()[randomGenerator.Next(0, allAvailableChars.Length)]);
            }

            var passwordCharArray = new Char[passwordLength];
            var emptyLocations    = Enumerable.Range(0, passwordLength).ToList();

            foreach (var selectedChar in selectedChars)
            {
                var emptyLocationIndex = randomGenerator.Next(0, emptyLocations.Count);
                passwordCharArray[emptyLocations[emptyLocationIndex]] = selectedChar;
                emptyLocations.RemoveAt(emptyLocationIndex);
            }

            Password.Set(context, string.Join("", passwordCharArray));
        }