You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

47 lines
1.3 KiB

using System;
using System.Collections.Concurrent;
using System.Text;
using TSingSoft.WebPluginFramework;
namespace BWP.B3DogAuth {
public class MobileAuthCenter {
static ConcurrentDictionary<string, Tuple<string, DateTime>> mDic = new ConcurrentDictionary<string, Tuple<string, DateTime>>();
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<string, DateTime> 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<string, DateTime> 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<string, DateTime>(builder.ToString(), BLContext.Now.AddMinutes(5));
}
}
}