using System; using System.Collections.Concurrent; using System.Text; using TSingSoft.WebPluginFramework; namespace BWP.B3DogAuth { public class MobileAuthCenter { static ConcurrentDictionary> mDic = new ConcurrentDictionary>(); public static string GenVerifyCode(string username) { var code = GenRandomCode(); var tuple = GenRandomCode(); mDic.AddOrUpdate(username, tuple, (arg1, arg2) => tuple); return tuple.Item1; } public static bool Auth(string username, string verifyCode) { Tuple tuple; if (mDic.TryGetValue(username, out tuple)) { if (tuple.Item2 < BLContext.Now) { mDic.TryRemove(username, out tuple); return false; } else if (tuple.Item1 == verifyCode) { return true; } else { return false; } } else { return false; } } static Tuple GenRandomCode() { var rand = new Random(BLContext.Now.GetHashCode()); var builder = new StringBuilder(); for (int i = 0; i < 4; i++) { builder.Append(rand.Next(9)); } return new Tuple(builder.ToString(), BLContext.Now.AddMinutes(5)); } } }