コード例 #1
0
ファイル: ServiceProvider.cs プロジェクト: terry2012/DSV
        /// <summary>
        /// Creates a cryptographically strong random verification code.
        /// </summary>
        /// <param name="format">The desired format of the verification code.</param>
        /// <param name="length">The length of the code.
        /// When <paramref name="format"/> is <see cref="VerificationCodeFormat.IncludedInCallback"/>,
        /// this is the length of the original byte array before base64 encoding rather than the actual
        /// length of the final string.</param>
        /// <returns>The verification code.</returns>
        public static string CreateVerificationCode(VerificationCodeFormat format, int length)
        {
            Requires.InRange(length >= 0, "length");

            switch (format)
            {
            case VerificationCodeFormat.IncludedInCallback:
                return(MessagingUtilities.GetCryptoRandomDataAsBase64(length));

            case VerificationCodeFormat.AlphaNumericNoLookAlikes:
                return(MessagingUtilities.GetRandomString(length, MessagingUtilities.AlphaNumericNoLookAlikes));

            case VerificationCodeFormat.AlphaUpper:
                return(MessagingUtilities.GetRandomString(length, MessagingUtilities.UppercaseLetters));

            case VerificationCodeFormat.AlphaLower:
                return(MessagingUtilities.GetRandomString(length, MessagingUtilities.LowercaseLetters));

            case VerificationCodeFormat.Numeric:
                return(MessagingUtilities.GetRandomString(length, MessagingUtilities.Digits));

            default:
                throw new ArgumentOutOfRangeException("format");
            }
        }
コード例 #2
0
		private void TestCode(VerificationCodeFormat format, int length, string allowableCharacters) {
			string code = ServiceProvider.CreateVerificationCode(format, length);
			TestUtilities.TestLogger.InfoFormat("{0} of length {2}: {1}", format, code, length);
			Assert.AreEqual(length, code.Length);
			foreach (char ch in code) {
				Assert.IsTrue(allowableCharacters.Contains(ch));
			}
		}
コード例 #3
0
ファイル: ServiceProviderTests.cs プロジェクト: terry2012/DSV
        private void TestCode(VerificationCodeFormat format, int length, string allowableCharacters)
        {
            string code = ServiceProvider.CreateVerificationCode(format, length);

            TestUtilities.TestLogger.InfoFormat("{0} of length {2}: {1}", format, code, length);
            Assert.AreEqual(length, code.Length);
            foreach (char ch in code)
            {
                Assert.IsTrue(allowableCharacters.Contains(ch));
            }
        }
コード例 #4
0
		/// <summary>
		/// Creates a cryptographically strong random verification code.
		/// </summary>
		/// <param name="format">The desired format of the verification code.</param>
		/// <param name="length">The length of the code.
		/// When <paramref name="format"/> is <see cref="VerificationCodeFormat.IncludedInCallback"/>,
		/// this is the length of the original byte array before base64 encoding rather than the actual
		/// length of the final string.</param>
		/// <returns>The verification code.</returns>
		public static string CreateVerificationCode(VerificationCodeFormat format, int length) {
			Requires.InRange(length >= 0, "length");

			switch (format) {
				case VerificationCodeFormat.IncludedInCallback:
					return MessagingUtilities.GetCryptoRandomDataAsBase64(length);
				case VerificationCodeFormat.AlphaNumericNoLookAlikes:
					return MessagingUtilities.GetRandomString(length, MessagingUtilities.AlphaNumericNoLookAlikes);
				case VerificationCodeFormat.AlphaUpper:
					return MessagingUtilities.GetRandomString(length, MessagingUtilities.UppercaseLetters);
				case VerificationCodeFormat.AlphaLower:
					return MessagingUtilities.GetRandomString(length, MessagingUtilities.LowercaseLetters);
				case VerificationCodeFormat.Numeric:
					return MessagingUtilities.GetRandomString(length, MessagingUtilities.Digits);
				default:
					throw new ArgumentOutOfRangeException("format");
			}
		}