示例#1
0
        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));
            }
        }
示例#2
0
        /// <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));
            }
        }