/// <summary> /// If it is single sign on , need to remove the existing token /// </summary> /// <param name="userId"></param> /// <returns></returns> public override SimpleAuthResult RemoveTokenFor(long userId) { try { using (var context = new SimpleUserDbContext()) { var userTokens = context.UserToken.Where(x => x.UserId == userId).ToList(); if (userTokens.Count > 0) { foreach (var userToken in userTokens) { context.UserToken.Remove(userToken); context.SaveChanges(); } } return(SimpleAuthResult.Success()); } } catch (Exception ex) { var error = string.Format("remove token failed for user '{0}'", userId); return(SimpleAuthResult.Fail(error)); } }
public override SimpleAuthResult VerifyToken(string accessToken) { try { UserToken record; using (var context = new SimpleUserDbContext()) { record = context.UserToken.FirstOrDefault(x => accessToken == x.AccessToken); string error = ""; if (record == null) { error = string.Format("AccessToken '{0}' not found", accessToken); return(SimpleAuthResult.Fail(error)); } if (record.ExpireAt < DateTime.Now) { error = string.Format("Token '{0}' has been expired.", accessToken); return(SimpleAuthResult.Fail(error)); } return(SimpleAuthResult.Success()); } } catch (Exception ex) { var error = string.Format("unknown error .{0}", ex.Message); return(SimpleAuthResult.Fail(error)); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { var accessToken = HttpContext.Current.Request[ACCESSTOKEN]; if (string.IsNullOrWhiteSpace(accessToken)) { filterContext.Result = new JsonResult() { Data = SimpleAuthResult.Fail("access token is empty.") }; return; } var resultToken = _service.TokenLogin(accessToken); if (!resultToken.IsSuccess) { filterContext.Result = new JsonResult() { Data = SimpleAuthResult.Fail(resultToken.Error) }; } // let it go ,passed }