当前位置: 首页 > news >正文

BS客户端的单点登录

1、参数类似于XXXXX://?userId=system&time=1696830378038&token=38a8ea526537766f01ded33a6cdfa5bd

2、在config里加一个LoginSecret参数可随意指定一个字符串

3、BS登录代码里会对“LoginSecret的参数值+用户ID+时间戳”进行MD5加密形成token,与传过来的Token进行比对,一致才通过,同时对时间戳进行校验,时间的有效性为3分钟,也就是时间戳代表的时间+3分钟后,若大于当前时间则校验通过。通过后,直接以传过来的用户名登录BS,否则弹出提示信息。

以下为示例代码:

public static class TokenUtil

{

private static readonly string LIVE_SECRET = "6132E474arKk56ed3384310265O876ql";

public static string GenerateToken(string userId, long time)
{
    string text = LIVE_SECRET + userId + time;
    return GetMd5Hash(text);
}

public static bool ValidateToken(string userId, string time, string token)
{
    if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(time) || string.IsNullOrEmpty(token))
    {
        return false;
    }
    if (TimestampToDateTime(time).AddMinutes(3) < DateTime.Now)
    {
        return false;
    }

    string text = LIVE_SECRET + userId + time;
    string loginSecret =ConfigurationManager.AppSettings["LoginSecret"];
    if (!string.IsNullOrEmpty(loginSecret))
        text = loginSecret + userId + time;

    string validateToken = GetMd5Hash(text);

    return token.Equals(validateToken, StringComparison.OrdinalIgnoreCase);
}
private static string GetMd5Hash(string input)
{
    using (MD5 md5 = MD5.Create())
    {
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("x2"));
        }

        return sb.ToString();
    }
}
/// <summary>
/// 本时区日期时间转时间戳
/// </summary>
/// <param name="datetime"></param>
/// <returns>long=Int64</returns>
public static long ToTimestamp(this DateTime datetime)
{
    DateTime dd = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    DateTime timeUTC = DateTime.SpecifyKind(datetime, DateTimeKind.Utc);//本地时间转成UTC时间
    TimeSpan ts = (timeUTC - dd);
    return (Int64)(ts.TotalMilliseconds);//精确到毫秒
}
/// <summary>
/// 时间戳转本时区日期时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime TimestampToDateTime(this string timeStamp)
{
    DateTime dd = DateTime.SpecifyKind(new DateTime(1970, 1, 1, 0, 0, 0, 0), DateTimeKind.Local);
    long longTimeStamp = long.Parse(timeStamp + "0000");
    TimeSpan ts = new TimeSpan(longTimeStamp);
    return dd.Add(ts);
}

}

调用:

public partial class Login

{

 private string strTempUser;

protected override void OnLoad( EventArgs e )

{

PageLoad("Login");

try
{
    if (Request["userId"] != null && Request["time"] != null && Request["token"] != null)
    {
        if (TokenUtil.ValidateToken(Request["userId"].ToString(), Request["time"].ToString(), Request["token"].ToString()))
        {
            strTempUser = Request["userId"].ToString();
            User u = UsersDirectory.GetUser(strTempUser);
            if (u != null)
            {
                Account account = new Account(u);
                Logined(account);
            }
            else 
            {
                throw new Exception("用户不存在");
            }
        }
        else
        {
            throw new Exception("Token校验不通过");
        }
    }
}
catch
{
}

}

}

相关文章:

  • 东南亚与中东小游戏市场出海调研报告
  • 7.0 sharpScada的sql数据的安装
  • 如何解决windows端口被占用
  • Kubernetes 节点 Not Ready 时 Pod 驱逐机制深度解析(下)
  • Java—— 常见API介绍 第四期
  • 【多目标进化算法】常见多目标进化算法一览
  • IP查询专业版:支持IPv4/IPv6自动识别并切换解析的API接口使用指南
  • C++ (STL,顺序容器,关联容器,容器适配器)
  • markdown自动标题序号,标题序号,目录处理
  • 软件设计模式与体系结构:基于Java实现管道-过滤器架构
  • RunnerGo API性能测试实战与高并发调优
  • SQL Server 2019 安装与配置详细教程
  • 区间和数量统计 之 前缀和+哈希表
  • Linux内核参数调优(TCP BBR算法实践)
  • 【计算机视觉】CV实践项目- 基于PaddleSeg的遥感建筑变化检测全解析:从U-Net 3+原理到工程实践
  • c++11新特性随笔
  • Flink部署与应用——部署方式介绍
  • 机器学习基础理论 - 判别模型 vs 生成模型
  • CNN卷积神经网络知识点回顾学习(一)
  • 安卓手机下载谷歌浏览器遇到兼容问题怎么办【三步解决】
  • 摩根士丹利基金雷志勇:AI带来的产业演进仍在继续,看好三大景气领域
  • 健康社区“免疫行动”促进计划启动,发布成人预防“保典”
  • 上海2025年普通高等学校招生志愿填报与投档录取实施办法公布
  • 美媒称特朗普考虑大幅下调对华关税、降幅或超一半,外交部回应
  • 特朗普:泽连斯基的言论对和平谈判非常有害
  • 宁德时代与广汽等五车企发布10款巧克力换电新车型:年内将完成30城1000站计划