2022年06月09日

图形验证码

后端生成的图片二维码保存为byte[](字节数组),字节数组转成base64编码字符串,返回前端字符串。

byte[] mData = CheckCodeByPicHelp.f_GetCode(4, ref mCode); // 长度, 验证码值
string checkcodepic = Convert.ToBase64String(mData);

Redis数据库中存 YanZhengMa:GUID(唯一值),值存验证码(2we3),过期时间秒;返回前端GUID。

redis.StringSet("YanZhengMa:03ae6bd2-610a-4fae-8aa9-bb3cc619c5db",
                "2we3", 
                TimeSpan.FromSeconds(60 * 60)
)

返回数据

{
  "code": 0,
  "message": "获取验证码成功",
  "data": {
    "codeid": "03ae6bd2-610a-4fae-8aa9-bb3cc619c5db",
    "checkcodepic": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAeAGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0d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B//2Q=="
  }
}

登录

登录传参为:用户名,密码,验证码,”codeid”: “11ecc80d-0f0b-49f5-8678-201f6768ce3e”,共4个参数。

  1. 验证码判断完后,从Redis数据库中删除。

  2. 根据用户名查询用户信息,再判断密码是否正确。

token:根据用户id,用户编码,用户名生成JWT。TODO:2022年6月9日23:35:48

session:Redis数据库中存 Session:GUID(唯一值),值存当前系统时间,登录过期时间秒;返回前端GUID。

redis.StringSet("Session:11ecc80d-0f0b-49f5-8678-201f6768ce3e", 
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),         
                TimeSpan.FromSeconds(7 * 24 * 60 * 60)
);

返回数据

{
  "code": 0,
  "message": "登录成功",
  "data": {
    "user_id": "001",
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiIwMDEiLCJ1c2VybmFtZSI6IndqbSIsImV4cCI6MTY1MzQ1NzEzNi4wfQ.CFOXrlS0QXr8Ge7G739vFBP03bNVYGtpM6NxDNn5SEM",
    "xwsessionid": "11ecc80d-0f0b-49f5-8678-201f6768ce3e"
  }
}

/// <summary>
/// 获取Token
/// </summary>
/// <param name="pUserInfo"></param>
/// <param name="mErr"></param>
/// <returns></returns>
public static string f_GetToken(TokenUserInfo pUserInfo, ref string pErr)
{
    try
    {
        IDateTimeProvider provider = new UtcDateTimeProvider();
        var now = provider.GetNow();

        //var unixEpoch = JwtValidator.UnixEpoch; // 1970-01-01 00:00:00 UTC
        var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
        secondsSinceEpoch = secondsSinceEpoch + S_TokenTimeOut;
        var payload = new Dictionary<string, object>
        {
            { "userid", pUserInfo.S_UserId },
            { "username", pUserInfo.S_UserName },
            { "exp", secondsSinceEpoch }
        };
        IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
        IJsonSerializer serializer = new JsonNetSerializer();
        IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
        IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
        var token = encoder.Encode(payload, S_Secret);
        return token;
    }
    catch (Exception ex)
    {
        pErr = "获取会话令牌异常:" + ex.Message;
        return "";
    }
}