136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using Jose;
|
|
using Common;
|
|
|
|
namespace CommonEntity
|
|
{
|
|
public class JWTData
|
|
{
|
|
/// <summary>
|
|
/// 主题
|
|
/// </summary>
|
|
public string sub { get; set; }
|
|
/// <summary>
|
|
/// 发行人
|
|
/// </summary>
|
|
public string iss { get; set; }
|
|
/// <summary>
|
|
/// 到期时间
|
|
/// </summary>
|
|
public long exp { get; set; }
|
|
/// <summary>
|
|
/// 用户
|
|
/// </summary>
|
|
public string aud { get; set; }
|
|
|
|
/// <summary>
|
|
/// 在此之前不可用
|
|
/// </summary>
|
|
public string nbf { get; set; }
|
|
|
|
/// <summary>
|
|
/// 发布时间
|
|
/// </summary>
|
|
public string iat { get; set; }
|
|
|
|
/// <summary>
|
|
///用于标识该JWT
|
|
/// </summary>
|
|
public string jti { get; set; }
|
|
|
|
public string username { get; set; }
|
|
}
|
|
public class ChuangWeiUser
|
|
{
|
|
public string UserName { get; set; }
|
|
public string Version { get; set; }
|
|
}
|
|
public class ReturnInfo
|
|
{
|
|
public int code { get; set; }
|
|
public string msg { get; set; }
|
|
public object data { get; set; }
|
|
}
|
|
public class TokenData
|
|
{
|
|
public string accessToken { get; set; }
|
|
public string refreshToken { get; set; }
|
|
}
|
|
|
|
public class SignKeyCommon
|
|
{
|
|
public readonly static string ChuangWei_JWTKey = "#这x是98一个uv永远$不可0能到的a一个数据*";
|
|
|
|
public static ReturnInfo GetTokenData(string username)
|
|
{
|
|
DateTime dt = DateTime.Now.AddMonths(6);
|
|
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
long l = Tools.GetCurrentTimeStamp(dt);
|
|
CommonEntity.JWTData j = new CommonEntity.JWTData();
|
|
j.sub = "创维电视登录";
|
|
j.exp = l;
|
|
j.aud = "创维电视accesstoken";
|
|
j.nbf = DateTime.Now.AddMinutes(3).ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
j.iat = ti;
|
|
j.jti = Guid.NewGuid().ToString("N");
|
|
j.username = username;
|
|
|
|
var secretKey = Encoding.UTF8.GetBytes(SignKeyCommon.ChuangWei_JWTKey);
|
|
string token = Jose.JWT.Encode(j, secretKey, JwsAlgorithm.HS256);
|
|
|
|
j.aud = "创维电视refresh_token";
|
|
j.jti = Guid.NewGuid().ToString("N");
|
|
string refresh_token = Jose.JWT.Encode(j, secretKey, JwsAlgorithm.HS256);
|
|
|
|
ReturnInfo r = new ReturnInfo();
|
|
r.code = 200;
|
|
r.msg = "登录成功";
|
|
r.data = new TokenData()
|
|
{
|
|
accessToken = token,
|
|
refreshToken = refresh_token
|
|
};
|
|
return r;
|
|
}
|
|
|
|
public static bool TokenValidate(string retoken, out JWTData data, out int code, out string msg)
|
|
{
|
|
data = new JWTData();
|
|
bool isv = false;
|
|
code = 200;
|
|
msg = "sucess";
|
|
|
|
byte[] secretKey = Encoding.UTF8.GetBytes(SignKeyCommon.ChuangWei_JWTKey);
|
|
JWTData MMU = Jose.JWT.Decode<JWTData>(retoken, secretKey, JwsAlgorithm.HS256);
|
|
if (MMU != null)
|
|
{
|
|
DateTime dt = Tools.GetCurrentDateTime(MMU.exp);
|
|
string username = MMU.username;
|
|
if (dt > DateTime.Now)
|
|
{
|
|
isv = true;
|
|
data = MMU;
|
|
}
|
|
else
|
|
{
|
|
isv = false;
|
|
code = 10042;
|
|
msg = "Token值已经过期";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isv = false;
|
|
code = 10021;
|
|
msg = "非法token";
|
|
}
|
|
|
|
return isv;
|
|
}
|
|
}
|
|
}
|