public override void OnActionExecuting(ActionExecutingContext filterContext) { TokenEntity cookieResult = TokenEntity.Default(); if (!HttpContext.Current.Request.Cookies.TryExtractFromRequestCookie(out cookieResult)) { filterContext.Result = new ViewResult() { ViewName = LoginView }; return; } if (cookieResult.IsEmpty()) { filterContext.Result = new ViewResult() { ViewName = LoginView }; return; } var resultToken = _service.TokenLoginAndRefresh(cookieResult.RefreshToken); if (!resultToken.IsSuccess) { filterContext.Result = new ViewResult() { ViewName = LoginView }; } //// update token value in cookie HttpContext.Current.Response.Cookies.SaveInResponseCookie(resultToken.Data.AccessToken, resultToken.Data.RefreshToken, resultToken.Data.UserId.ToString()); }
public override RefreshTokenResult UpdateRefresh(string refreshToken) { try { UserToken record; using (var context = new SimpleUserDbContext()) { record = context.UserToken.FirstOrDefault(x => refreshToken == x.RefreshToken); string error = ""; if (record == null) { error = string.Format("RefreshToken '{0}' not found", refreshToken); return(new RefreshTokenResult(false, error, TokenEntity.Default())); } if (record.ExpireAt < DateTime.Now) { error = string.Format("Token '{0}' has been expired.", refreshToken); return(new RefreshTokenResult(false, error, TokenEntity.Default())); } ////create new one var newToken = CreateRefresh(); newToken.UserId = record.UserId; record.AccessToken = newToken.AccessToken; record.RefreshToken = newToken.RefreshToken; record.ExpireAt = DateTime.Parse(newToken.ExpireAt); context.SaveChanges(); return(new RefreshTokenResult(true, "", newToken)); } } catch (Exception ex) { var error = string.Format("unknown error .{0}", ex.Message); return(new RefreshTokenResult(false, error, TokenEntity.Default())); } }