Exemplo n.º 1
0
        /// <summary>
        /// 注销
        /// </summary>
        public static void Logout()
        {
            if (HttpContext.Current.User == null || !HttpContext.Current.User.Identity.IsAuthenticated)
            {
                throw new InvalidOperationException("Logout 操作必须授权");
            }

            var token = (HttpContext.Current.User.Identity as UserIdentity).Token;

            HttpCacheManager.RemoveCache(token);
            HttpContext.Current.User = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 设置安全主体
        /// </summary>
        /// <param name="identity"></param>
        internal static void SetPrincipal(UserIdentity identity)
        {
            var principal = new UserPrincipal(identity);

            HttpContext.Current.User = principal;
            if (identity.Expired == 0)
            {
                HttpCacheManager.SetCache(identity.Token, identity);
            }
            else
            {
                HttpCacheManager.SetCache(identity.Token, identity, identity.Expired);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 是有效的令牌
        /// </summary>
        /// <returns></returns>
        internal static bool IsValidToken(string token)
        {
            try
            {
                if (null == token)
                {
                    return(false);
                }

                var identity = HttpCacheManager.GetCache <UserIdentity>(token);
                return(null != identity);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 取用户令牌对应的用户标识
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        internal static UserIdentity GetIdentity(string token)
        {
            var identity = HttpCacheManager.GetCache <UserIdentity>(token);

            return(identity);
        }