static void Main(string[] args) { var configuration = CTDbContext.GetConfigurations(); //setup our DI var serviceProvider = new ServiceCollection() .AddLogging(builder => { builder.AddConsole(); builder.AddFilter <Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider> ("", LogLevel.Warning); builder.AddApplicationInsights(configuration.GetValue <string>("APPLICATION_INSIGHTS_KEY")); }) .AddOptions() .Configure <AppSettings>(configuration) .AddDbContext <CTDbContext>(options => options.UseLazyLoadingProxies().UseNpgsql(CTDbContext.ConnectionStringBuilder())) .AddScoped <SlackLogger>() .AddSingleton <MSTranscriptionService>() .AddSingleton <TempCode>() .AddSingleton <RpcClient>() .BuildServiceProvider(); Globals.appSettings = serviceProvider.GetService <IOptions <AppSettings> >().Value; TempCode tempCode = serviceProvider.GetService <TempCode>(); tempCode.Temp(); }
//获取单例模式 public static TempCode getInstance() { if (mTc == null) { mTc = new TempCode(); } return(mTc); }
static TimeSpan _expireLength = TimeSpan.FromMinutes(10); // 10 分钟 // 准备手机短信验证登录的第一阶段:产生验证码 // return: // -1 出错 // 0 沿用以前的验证码 // 1 用新的验证码 public int PrepareTempPassword( TempCodeCollection table, string strClientIP, string strPhoneNumber, out TempCode code, out string strError) { strError = ""; code = null; if (string.IsNullOrEmpty(strPhoneNumber)) { strError = "strPhoneNumber 参数值不应为空"; return -1; } strPhoneNumber = strPhoneNumber.Trim(); if (string.IsNullOrEmpty(strPhoneNumber)) { strError = "strPhoneNumber 参数值不应为空(1)"; return -1; } string strList = GetPhoneNumberBindingString(); if (string.IsNullOrEmpty(strList)) { strError = "当前账号未曾做过手机短信方式(sms:)绑定"; return -1; // 没有做过 sms: 绑定 } List<string> list = StringUtil.SplitList(strList, '|'); if (list.IndexOf(strPhoneNumber) == -1) { strError = "所提供的电话号码 '" + strPhoneNumber + "' 不在手机绑定号码列表中"; return -1; // 电话号码没有在列表中 } // 检索看看是否有已经存在的密码 bool bExist = false; DateTime now = DateTime.Now; string strKey = this.UserID + "|" + strPhoneNumber + "|" + strClientIP; code = table.FindTempCode(strKey); if (code != null) { if (code.ExpireTime < now) code = null; // 迫使重新取号 else { // 失效期还没有到。主动延长一次失效期 code.ExpireTime = DateTime.Now + _expireLength; bExist = true; } } if (code == null) { // 重新设定一个密码 Random rnd = new Random(); code = new TempCode(); code.Key = strKey; code.Code = rnd.Next(1, 999999).ToString(); code.ExpireTime = DateTime.Now + _expireLength; } table.SetTempCode(code.Key, code); // strTempCode = code.Code; if (bExist) return 0; return 1; }