using Nirvana.Common; using Nirvana.Data; using System; using System.Collections.Generic; using System.Text; using YBDevice.Entity; namespace YBDevice.Service.DBServices { /// /// 接口权限管理 /// public partial class BaseApplication : Repository { public static int ExpiresMin = 60 * 24*7;//60*24*1 登录过期时间,单位为分钟 public static WebApiOperaModel authInfo = null; public static string secureKey = Configs.GetString("SecureKey"); /// /// 生成token /// /// /// //public static string Token(WebApiOperaModel model) //{ // //生成token,SecureKey是用于加密token的key // byte[] key = Encoding.Default.GetBytes(secureKey); // IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // IJsonSerializer serializer = new JsonNetSerializer(); // IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); // IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); // model.expiretime = DateTime.Now.AddMinutes(ExpiresMin); // model.gid = Guid.NewGuid().ToString("N"); // //保存gid到redis // //RedisHelpers.Insert($"sc_{model.userid}", model.gid); // var token = encoder.Encode(model, key); // return token; //} /// /// token解密 /// /// /// 接口访问ip /// //public static bool DecodeToken(string token, string ip) //{ // if (string.IsNullOrEmpty(token)) // { // return false; // } // IJsonSerializer serializer = new JsonNetSerializer(); // var provider = new UtcDateTimeProvider(); // IJwtValidator validator = new JwtValidator(serializer, provider); // IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); // IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric // IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm); // var tokens = decoder.Decode(token, secureKey, verify: true); // if (string.IsNullOrEmpty(tokens)) // { // return false; // } // var info = tokens.ToObject(); // //检查时间是否已经过期 // if (DateTime.Now > info.expiretime) // { // return false; // } // //检查用户ID是否存在 // if (info.UserId <=0) // { // return false; // } // //if (RedisHelpers.stringGet($"sc_{ info.userid}") != info.gid) // //{ // // return false; // //} // //检查访问的ip是否为token里的ip // //if (ip != info.loginip) // //{ // // return false; // //} // info.expiretime = DateTime.Now.AddMinutes(ExpiresMin); // authInfo = info; // return true; //} } }