Exemplo n.º 1
0
        public static List <SystemUser> GetByRole(string rolename, string area)
        {
            List <SystemUser> _users = new List <SystemUser>();
            //NetCacheHelper
            string            cacheKey = "GetByRole_" + rolename + area;
            List <SystemUser> users    = NetCacheHelper.Get <List <SystemUser> >(cacheKey);

            if (users == null)
            {
                using (Context db = new Context())
                {
                    _users = (from o in db.SystemUsers
                              join r in db.RoleUsers on o.Id equals r.UserId
                              join role in db.Roles on r.RoleId equals role.Id
                              where role.Name.Contains(rolename) && role.Name.Contains(area) && o.State != 1
                              select o).ToList();
                }
                NetCacheHelper.Add(cacheKey, _users, DateTime.Now.AddHours(10));
            }
            else
            {
                _users = users;
            }
            return(_users);
        }
        private string AuthExtendTime(TokenApplyPlace apiToken, string token, int _tokenExtendedTime_, AuthStatus authStatus)
        {
            if (AutoTokenExtended && !String.IsNullOrWhiteSpace(token) && authStatus == AuthStatus.Authorized)
            {
                //無cache 及 過期前 (_tokenExtendedTime/10)秒 提早SQL update
                int SlidingExpiration = -1 * (int)Math.Round(_tokenExtendedTime_ / 10.0);
                if (apiToken == null || apiToken.ExpireDateTime.AddSeconds(SlidingExpiration) < DateTime.Now)
                {
                    using (SqlConnection conn = new SqlConnection(_DBconfig)) {
                        string sqlStr = @"UPDATE TOP (1) dbo.Token
                                        SET RefreshDateTime = GETDATE(),
                                            ExpireDateTime = DATEADD(s, @tokenExtendedTime, GETDATE())
                                        OUTPUT INSERTED.RefreshDateTime, INSERTED.ExpireDateTime
                                        WHERE Token = @token";

                        conn.Open();
                        var updateTmp = conn.Query <TokenApplyPlace>(sqlStr, new { token = token, tokenExtendedTime = _tokenExtendedTime_ }).SingleOrDefault();
                        if (updateTmp != null)
                        {
                            _apiToken.RefreshDateTime = updateTmp.RefreshDateTime;
                            _apiToken.ExpireDateTime  = updateTmp.ExpireDateTime;

                            if (_apiToken != null && _apiToken.ExpireDateTime != null)
                            {
                                NetCacheHelper.Add(token, _apiToken, _apiToken.ExpireDateTime);
                            }
                        }
                    }
                }
                return(_apiToken.ExpireDateTime.ToString("yyyy-MM-ddTHH:mm:ssK"));
            }
            return(null);
        }