예제 #1
0
        private static string GetSingleOrDefaultClaimValue(this IPrincipal principal, string claimName)
        {
            try
            {
                return(principal.GetClaims()
                       .SingleOrDefault(claim => claim.Type == claimName)?.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 claims = principal.GetClaims();

                    if (claims == null)
                    {
                        Trace.TraceError($"{principalName}: {methodName}: claims is NULL");
                    }
                }

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
예제 #2
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);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        private static IEnumerable <Claim> GetClaims(this IPrincipal principal)
        {
            try
            {
                return(principal.GetClaimsIdentity().Claims);
            }
            catch (Exception exc)
            {
                var principalName = principal.GetName();
                var methodName    = MethodBase.GetCurrentMethod().Name;

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

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
예제 #5
0
        public void GetName_UsingValidPrincipal_MustReturnExpectedName(IPrincipal validPrincipal)
        {
            // Arrange & Act
            string userName = validPrincipal.GetName();

            // Assert
            userName.Should().NotBeNullOrWhiteSpace("because principal name must means something");
        }
예제 #6
0
        public void GetName_UsingNullAsPrincipalIdentity_MustThrowException()
        {
            // Arrange
            var principalMock = new Mock <IPrincipal>();

            principalMock.SetupGet(principal => principal.Identity).Returns(() => null);
            IPrincipal mockedPrincipal = principalMock.Object;

            // Act
            Action actionExpectedToFail = () => mockedPrincipal.GetName();

            // Assert
            actionExpectedToFail.Should().ThrowExactly <ArgumentNullException>()
            .And.ParamName.Should().Be("principal.Identity",
                                       "because a principal with a null identity does not have a name");
        }
예제 #7
0
        public static string GetDomain(this IPrincipal principal)
        {
            try
            {
                return(principal.GetSingleOrDefaultClaimValue(CustomClaimTypes.Domain));
            }
            catch (Exception exc)
            {
                var principalName = principal.GetName();
                var methodName    = MethodBase.GetCurrentMethod().Name;

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

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
예제 #8
0
        public static List <string> GetSamplesRoles(this IPrincipal principal)
        {
            try
            {
                return(principal.GetClaims()
                       .Where(claim => claim.Type == ClaimTypes.GroupSid)
                       .Select(claim =>
                               new
                {
                    claim,
                    roleName = new SecurityIdentifier(claim.Value).Translate(typeof(NTAccount)).Value
                })
                       .Where(x => x.roleName.StartsWith(ActiveDirectorySettings.DomainAndPrefix,
                                                         StringComparison.InvariantCultureIgnoreCase))
                       .Select(x => x.roleName)
                       .ToList());
            }
            catch (Exception exc)
            {
                var principalName = principal.GetName();
                var methodName    = MethodBase.GetCurrentMethod().Name;

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

                LogException(principalName, methodName, exc);
                return(null);
            }
        }
        private async Task <TodoItem> GetExistingTodoItem(long?id, IPrincipal owner)
        {
            TodoItem existingTodoItem = await todoDbContext.TodoItems
                                        .SingleOrDefaultAsync(todoItem => todoItem.Id == id && todoItem.CreatedBy == owner.GetName());

            if (existingTodoItem == null)
            {
                throw new EntityNotFoundException(typeof(TodoItem), id);
            }

            return(existingTodoItem);
        }