예제 #1
0
        /// <summary>
        /// Convert auth session to 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   = SessionConfiguration.GetSessionConfiguration();
            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>
 /// Get subject
 /// </summary>
 /// <param name="claims">Claims</param>
 /// <returns>Return subject</returns>
 public static string GetSubject(IEnumerable <Claim> claims)
 {
     if (claims != null && claims.Any())
     {
         var sessionConfig = SessionConfiguration.GetSessionConfiguration();
         return(GetSubject(claims.ToDictionary(c => c.Type, c => c.Value)));
     }
     return(string.Empty);
 }
예제 #3
0
 /// <summary>
 /// Gets session token
 /// </summary>
 /// <param name="claims">Claims</param>
 /// <returns>Return session token</returns>
 public static string GetSessionToken(IEnumerable <Claim> claims)
 {
     if (claims != null && claims.Any())
     {
         var sessionConfig = SessionConfiguration.GetSessionConfiguration();
         var sessionClaim  = claims.FirstOrDefault(c => c.Type == sessionConfig.SessionClaimName);
         return(sessionClaim?.Value ?? string.Empty);
     }
     return(string.Empty);
 }
예제 #4
0
        /// <summary>
        /// Get session token
        /// </summary>
        /// <param name="values">Values</param>
        /// <returns>Return session token</returns>
        public static string GetSessionToken(Dictionary <string, string> values)
        {
            if (values == null || values.Count <= 0)
            {
                return(string.Empty);
            }
            var sessionConfig = SessionConfiguration.GetSessionConfiguration();

            values.TryGetValue(sessionConfig.SessionClaimName, out string sessionToken);
            return(sessionToken);
        }
예제 #5
0
        /// <summary>
        /// Get auth session by authentication ticket
        /// </summary>
        /// <param name="ticket">Ticket</param>
        /// <returns>Return auth session</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 = SessionConfiguration.GetSessionConfiguration();

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