Пример #1
0
        /// <summary>
        /// 将session对象转换为Ticket
        /// </summary>
        /// <returns></returns>
        public AuthenticationTicket ConvertToTicket()
        {
            var claimIdentity = new ClaimsIdentity(AuthenticationScheme);

            claimIdentity.AddClaims(Claims.Select(c => new Claim(c.Key, c.Value)));
            var sessionConfig   = SessionConfig.GetSessionConfig();
            var nowSessionClaim = claimIdentity.Claims.FirstOrDefault(c => c.Type == sessionConfig.SessionClaimName);

            if (nowSessionClaim == null)
            {
                claimIdentity.AddClaim(new Claim(sessionConfig.SessionClaimName, SessionToken));
            }
            AuthenticationProperties props = new AuthenticationProperties();

            if (PropertiesItems != null)
            {
                foreach (var pitem in PropertiesItems)
                {
                    props.Items.Add(pitem.Key, pitem.Value);
                }
            }
            props.IsPersistent = true;
            props.AllowRefresh = true;
            return(new AuthenticationTicket(new ClaimsPrincipal(claimIdentity), props, AuthenticationScheme));
        }
Пример #2
0
 /// <summary>
 /// 获取授权对象
 /// </summary>
 /// <param name="claims">凭据信息</param>
 /// <returns></returns>
 public static string GetSubject(IEnumerable <Claim> claims)
 {
     if (claims != null && claims.Any())
     {
         var sessionConfig = SessionConfig.GetSessionConfig();
         return(GetSubject(claims.ToDictionary(c => c.Type, c => c.Value)));
     }
     return(string.Empty);
 }
Пример #3
0
 /// <summary>
 /// 获取session键值
 /// </summary>
 /// <param name="claims">凭据信息</param>
 /// <returns></returns>
 public static string GetSessionToken(IEnumerable <Claim> claims)
 {
     if (claims != null && claims.Any())
     {
         var sessionConfig = SessionConfig.GetSessionConfig();
         var sessionClaim  = claims.FirstOrDefault(c => c.Type == sessionConfig.SessionClaimName);
         return(sessionClaim?.Value ?? string.Empty);
     }
     return(string.Empty);
 }
Пример #4
0
        /// <summary>
        /// 获取session键值
        /// </summary>
        /// <param name="values">值</param>
        /// <returns></returns>
        public static string GetSessionToken(Dictionary <string, string> values)
        {
            if (values == null || values.Count <= 0)
            {
                return(string.Empty);
            }
            var sessionConfig = SessionConfig.GetSessionConfig();

            values.TryGetValue(sessionConfig.SessionClaimName, out string sessionToken);
            return(sessionToken);
        }
Пример #5
0
        /// <summary>
        /// 获取session配置
        /// </summary>
        /// <returns></returns>
        public static SessionConfig GetSessionConfig()
        {
            SessionConfig sessionConfig = null;

            if (ContainerManager.IsRegister <IOptions <SessionConfig> >())
            {
                sessionConfig = ContainerManager.Resolve <IOptions <SessionConfig> >()?.Value;
            }
            if (sessionConfig == null)
            {
                sessionConfig = new SessionConfig();
            }
            return(sessionConfig);
        }
Пример #6
0
        /// <summary>
        /// AuthenticationTicket转换为Session对象
        /// </summary>
        /// <param name="ticket"></param>
        /// <returns></returns>
        public static AuthSession FromAuthenticationTicket(AuthenticationTicket ticket)
        {
            if (ticket == null)
            {
                return(null);
            }
            var session = new AuthSession()
            {
                AuthenticationScheme = ticket.AuthenticationScheme,
                PropertiesItems      = ticket.Properties.Items.ToDictionary(c => c.Key, c => c.Value),
                Claims = ticket.Principal.Claims?.ToDictionary(c => c.Type, c => c.Value),
            };
            var sessionConfig = SessionConfig.GetSessionConfig();

            if (session.Claims?.ContainsKey(sessionConfig.SessionClaimName) ?? false)
            {
                session.SessionToken = session.Claims[sessionConfig.SessionClaimName];
            }
            session.Expires = DateTimeOffset.Now.Add(sessionConfig.Expires);
            return(session);
        }