Ejemplo n.º 1
0
        private static string FindFirstClaimValue(this IPrincipal principal, string claim)
        {
            try
            {
                return(principal.GetClaimsPrincipal().FindFirst(claim)?.Value);
            }
            catch (Exception exc)
            {
                var principalName = principal.GetName();
                var methodName    = MethodBase.GetCurrentMethod().Name;

                if (principal == null)
                {
                    Trace.TraceError($"{principalName}: {methodName}: principal is NULL");
                }
                else
                {
                    var claimsPrincipal = principal.GetClaimsPrincipal();
                    if (claimsPrincipal == null)
                    {
                        Trace.TraceError($"{principalName}: {methodName}: claimsPrincipal is NULL");
                    }
                }

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static ClaimsIdentity GetClaimsIdentity(this IPrincipal principal)
        {
            try
            {
                return((ClaimsIdentity)principal.GetClaimsPrincipal().Identity);
            }
            catch (Exception exc)
            {
                var principalName = principal.GetName();
                var methodName    = MethodBase.GetCurrentMethod().Name;

                if (principal == null)
                {
                    Trace.TraceError($"{principalName}: {methodName}: principal is NULL");
                }
                else
                {
                    var claimsPrincipal = principal.GetClaimsPrincipal();
                    if (claimsPrincipal == null)
                    {
                        Trace.TraceError($"{principalName}: {methodName}: claimsPrincipal is NULL");
                    }
                }

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
Ejemplo n.º 3
0
        public static string GetClaim(this IPrincipal principal, string name)
        {
            var current = principal.GetClaimsPrincipal();
            var claim   = current.FindFirst(name);

            return(claim?.Value);
        }
Ejemplo n.º 4
0
        public static ClaimsIdentity GetClaimsIdentity(this IPrincipal principal)
        {
            var claimsPrincipal = principal.GetClaimsPrincipal();

            if (claimsPrincipal == null)
            {
                return(null);
            }

            var identity = claimsPrincipal.Identity as ClaimsIdentity;

            if (identity == null)
            {
                return(null);
            }

            return(identity);
        }