/// <summary> /// Returns a random element from a list, or null if the list is empty. /// </summary> /// <typeparam name="T">The type of object being enumerated</typeparam> /// <param name="rand">An instance of a random number generator</param> /// <returns>A random element from a list, or null if the list is empty</returns> public static T Random <T>(this IEnumerable <T> list, CryptoRandomGenerator rand) { if (list != null && list.Count() > 0) { return(list.ElementAt(rand.Next(list.Count()))); } return(default(T)); }
Task <string> IUserTokenProvider <BackOfficeIdentityUser, int> .GenerateAsync(string purpose, UserManager <BackOfficeIdentityUser, int> manager, BackOfficeIdentityUser user) { lock (syncLock) { var code = _random.Next(999999).ToString(); return(Task.FromResult(code)); } }
/// <summary> /// Shuffles an IList in place. /// </summary> /// <typeparam name="T">The type of elements in the list</typeparam> /// <param name="rand">An instance of a random number generator</param> public static void Shuffle <T>(this IList <T> list, CryptoRandomGenerator rand) { int count = list.Count; while (count > 1) { int i = rand.Next(count--); T temp = list[count]; list[count] = list[i]; list[i] = temp; } }
public HttpResponseMessage SetupSMS(string number) { var user = SignInManager.GetVerifiedUserId(); var details = CustomDatabase.GetUserDetails(user); if (details != null && details.IsValidated) { throw new UnauthorizedAccessException("This account has already setup SMS"); } var isNew = details == null; details = new FortressUser2FASettings(); details.UserId = user; details.Provider = "SMS"; details.Configuration = number; details.IsValidated = false; lock (syncLock) { var code = _random.Next(999999).ToString(); details.CurrentCode = code; details.CurrentCodeGenerated = DateTime.UtcNow; } if (isNew) { CustomDatabase.Insert(details); } else { CustomDatabase.Update(details); } var settings = CustomDatabase.GetSettingsFromDatabase(); var SmsProvider = FortressContext.GetCurrentSmsProvider(); SmsProvider.SendSms(details.Configuration, string.Format(settings.SMS_MessageFormat, details.CurrentCode)); var response = Request.CreateResponse(HttpStatusCode.OK, new { token = "123456" }); return(response); }
public static string NewSecurePassword() { var UpperCharsSet = Enumerable.Range(65, 26).Select(i => (byte)i).ToArray(); // A..Z var LowerCharsSet = Enumerable.Range(97, 26).Select(i => (byte)i).ToArray(); // a..z var NumbersSet = Enumerable.Range(48, 10).Select(i => (byte)i).ToArray(); // 0..9 var SpecialCharsSet = new byte[] { 33, 35, 40, 41, 64 }; // !, #, (, ), @ var All = UpperCharsSet.Concat(LowerCharsSet).Concat(NumbersSet).Concat(SpecialCharsSet).ToArray(); using (var r = new CryptoRandomGenerator()) { int length = r.Next(32, 41); var u = Enumerable.Range(0, 5).Select(i => UpperCharsSet[r.Next(0, UpperCharsSet.Length)]); var l = Enumerable.Range(0, 1).Select(i => LowerCharsSet[r.Next(0, LowerCharsSet.Length)]); var n = Enumerable.Range(0, 1).Select(i => NumbersSet[r.Next(0, NumbersSet.Length)]); var s = Enumerable.Range(0, 4).Select(i => SpecialCharsSet[r.Next(0, SpecialCharsSet.Length)]); var a = Enumerable.Range(0, length - 11).Select(i => All[r.Next(0, All.Length)]); return(Encoding.ASCII.GetString(u.Concat(l).Concat(n).Concat(s).Concat(a).Shuffle().ToArray())); } }