-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathComplexHelperTest.cs
More file actions
165 lines (146 loc) · 7.21 KB
/
Copy pathComplexHelperTest.cs
File metadata and controls
165 lines (146 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using CheckCodeHelper.Sender.AlibabaSms;
using CheckCodeHelper.Sender.EMail;
using CheckCodeHelper.Sender.Sms;
using CheckCodeHelper.Storage.Memory;
using CheckCodeHelper.Storage.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CheckCodeHelper.Samples
{
public static class ComplexHelperTest
{
static IServiceCollection services;
static IConfiguration configuration;
static ComplexHelperTest()
{
var basePath = Path.GetDirectoryName(typeof(ComplexHelperTest).Assembly.Location);
services = new ServiceCollection();
configuration = new ConfigurationBuilder()
.AddJsonFile(Path.Combine(basePath, "appsettings.json"), false)
.Build();
Init();
}
public static void Init()
{
services.AddOptions();
//注册ICodeSender
services.AddSingletonForNoneSender();
services.AddSingletonForSmsSenderWithEmay(configuration.GetSection("EmaySetting"));
services.AddSingletonForEMailSender(configuration.GetSection("EMailSetting"), configuration.GetSection("EMailMimeMessageSetting"));
services.AddSingletonForAlibabaSms(configuration.GetSection("AlibabaConfig"), configuration.GetSection("AlibabaSmsParameterSetting"));
//增加自定义ICodeSender例子,且该Sender未设置周期限制
services.AddSingleton<ICodeSender, ConsoleSender>();
//注册ICodeStorage
services.AddSingletonForRedisStorage(configuration.GetValue<string>("Redis:Configuration"));
//services.AddSingletonForMemoryCacheStorage();
//注册ComplexHelper
services.AddSingletonForComplexHelper(configuration.GetSection("ComplexSetting"));
}
public static void Start()
{
var serviceProvider = services.BuildServiceProvider();
//获取Helper,如果默认的InitComplexContentFormatter不符合业务需求,可继承后重写
//ComplexHelper依赖ICodeSender.Key来获取实际的验证码发送者,如果找不到,会产生异常
var complexHelper = serviceProvider.GetRequiredService<ComplexHelper>();
var bizFlag = configuration.GetValue<string>("BizFlag");
if (bizFlag == "LoginValidError")
{
LoginError(complexHelper);
return;
}
else
{
CodeValidator(complexHelper);
}
}
private static void CodeValidator(ComplexHelper complexHelper)
{
Console.WriteLine("****** 您当前正在进行验证码校验Demo ******");
var bizFlag = configuration.GetValue<string>("BizFlag");
var senderKey = configuration.GetValue<string>("CurrentSenderKey");
var receiver = configuration.GetValue<string>("Receiver");
var code = CodeHelper.GetRandomNumber(); //生成随机的验证码
Action getTimeAction = () =>
{
var time = complexHelper.CodeStorage.GetLastSetCodeTimeAsync(receiver, bizFlag).Result;
if (time.HasValue)
{
Console.WriteLine("上次发送时间:{0:yy-MM-dd HH:mm:ss.fff}", time.Value.LocalDateTime);
}
else
{
Console.WriteLine("未能获取到最后一次发送时间");
}
var ts = complexHelper.GetSendCDAsync(senderKey, receiver, bizFlag).Result;
Console.WriteLine("校验码发送剩余CD时间:{0}秒", ts.TotalSeconds);
};
getTimeAction();
var sendResult = complexHelper.SendCodeAsync(senderKey, receiver, bizFlag, code).Result;
Console.WriteLine("发送结果:{0} 请求时间:{1:yy-MM-dd HH:mm:ss}", sendResult, DateTime.Now);
if (sendResult == SendResult.Success || sendResult == SendResult.IntervalLimit)
{
Console.WriteLine("*****************************");
while (true)
{
Console.WriteLine("请输入校验码:");
var vCode = Console.ReadLine();
if (string.IsNullOrWhiteSpace(vCode)) continue;
getTimeAction();
var vResult = complexHelper.VerifyCodeAsync(senderKey, receiver, bizFlag, vCode).Result;
Console.WriteLine("{2:yy-MM-dd HH:mm:ss }校验码 {0} 校验结果:{1}", vCode, vResult, DateTime.Now);
if (vResult != VerificationResult.Failed)
{
break;
}
}
}
}
private static void LoginError(ComplexHelper complexHelper)
{
Console.WriteLine("****** 您当前正在进行密码校验错误Demo ******");
var senderKey = NoneSender.DefaultKey; //密码校验错误无需实际发送内容
var bizFlag = configuration.GetValue<string>("BizFlag");
var correct = "1"; //密码正确时,得到的code
var error = "0"; //密码错误时,得到的code
var correctPwd = "123456";//这里不管输入的是什么账号,密码假设为同一个密码
Console.WriteLine("所有账户的正确密码为:" + correctPwd);
do
{
var account = ReadLine("** 请输入您的账号 **", "账号不能为空");
var pwd = ReadLine("** 请输入您的密码 **", "密码不能为空");
//检验前进行一次发送,因为周期设置成只能发送一次,所以无需关心发送结果
var sendResult = complexHelper.SendCodeAsync(senderKey, account, bizFlag, correct).Result;
Console.WriteLine("周期设置结果:" + sendResult + " 注意周期设置结果不影响校验");
var code = GetCode(pwd);
var verifyResult = complexHelper.VerifyCodeAsync(senderKey, account, bizFlag, code, true).Result;//密码校验正确时,重置允许密码连续错误的次数
Console.WriteLine("您账号 {0} 的密码验证结果 {1}", account, verifyResult);
}
while (true);
string GetCode(string pwd)
{
return pwd == correctPwd ? correct : error;
}
string ReadLine(string title, string errorNotice)
{
Console.WriteLine(title);
do
{
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine(errorNotice);
}
else
{
return input;
}
}
while (true);
}
}
}
}