// GET: /<controller>/ public async Task<IActionResult> Index() { string userCode = string.Empty; string totenID = string.Empty; if (!string.IsNullOrEmpty(Request.Query["token"]) && Request.Query["token"].Any() && !string.IsNullOrEmpty(Request.Query["userCode"]) && Request.Query["userCode"].Any()) { userCode = Request.Query["userCode"][0]; totenID = Request.Query["token"][0]; // HttpContext.Response.Cookies.Append("",""); var identity = new ClaimsIdentity("UserForm"); // 指定身份认证类型 identity.AddClaim(new Claim(ClaimTypes.Sid, userCode)); // 键值对 identity.AddClaim(new Claim(ClaimTypes.Name, totenID)); // 键值对 var principal = new ClaimsPrincipal(identity); await HttpContext.Authentication.SignInAsync("Cookie", principal, new AuthenticationProperties { IsPersistent = true }); } else if (User.FindFirst(ClaimTypes.Sid) != null) { totenID = User.FindFirst(ClaimTypes.Sid).Value; } if (!string.IsNullOrEmpty(totenID)) { //远程sso 服务中心验证 数据的有效性 UserTotenInput userInput = new UserTotenInput() { UserCode = userCode, Toten = totenID }; var flag = HttpClientHeaper<UserTotenInput>.Post(StaticParameter.ValidateToken, userInput); if (flag == null) { //跳转登录页面 return Redirect("http://www.baidu.com/Home/index"); } } else { //跳转登录页面 } return View(); }
public bool KeepToken(UserTotenInput user) { bool flag = true; if (_memoryCache.Get <string>(user.UserCode) == user.Toten) { _memoryCache.Remove(user.UserCode); } else { flag = false; } return(flag); }
public bool CheckAndGetToten([FromBody] UserTotenInput user) { bool flag = false; if (!string.IsNullOrEmpty(user.UserCode) && !string.IsNullOrEmpty(user.Toten)) { string result; if (!_memoryCache.TryGetValue(user.UserCode, out result)) { //设置相对过期时间2分钟 _memoryCache.Set(user.UserCode, user.Toten, new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(2))); ////设置绝对过期时间2分钟 //_memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() // .SetAbsoluteExpiration(TimeSpan.FromMinutes(2))); ////移除缓存 //_memoryCache.Remove(cacheKey); ////缓存优先级 (程序压力大时,会根据优先级自动回收) //_memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() // .SetPriority(CacheItemPriority.NeverRemove)); ////缓存回调 10秒过期会回调 //_memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() // .SetAbsoluteExpiration(TimeSpan.FromSeconds(10)) // .RegisterPostEvictionCallback((key, value, reason, substate) => // { // Console.WriteLine($"键{key}值{value}改变,因为{reason}"); // })); ////缓存回调 根据Token过期 //var cts = new CancellationTokenSource(); //_memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() // .AddExpirationToken(new CancellationChangeToken(cts.Token)) // .RegisterPostEvictionCallback((key, value, reason, substate) => // { // Console.WriteLine($"键{key}值{value}改变,因为{reason}"); // })); //cts.Cancel(); flag = true; } } return(flag); }
public bool ValidateToken(UserTotenInput user) { bool flag = false; if (!string.IsNullOrEmpty(user.UserCode) && !string.IsNullOrEmpty(user.Toten)) { //判断 toten 是否有效 if (_memoryCache.Get <string>(user.UserCode) == user.Toten) { flag = true; } else { flag = false; } } else { flag = false; } return(flag); }