Exemplo n.º 1
0
        public void Current_FallsBackToThread_NoPrincipalPolicy()
        {
            RemoteExecutor.Invoke(() =>
            {
                ClaimsPrincipal principal1 = new ClaimsPrincipal();
                ClaimsPrincipal principal2 = new ClaimsPrincipal();

                Thread.CurrentPrincipal = principal1;
                Assert.Same(principal1, ClaimsPrincipal.Current);

                Thread.CurrentPrincipal = principal2;
                Assert.Same(principal2, ClaimsPrincipal.Current);

                NonClaimsIdentity id = new NonClaimsIdentity()
                {
                    Name = "NonClaimsIdentity_Name"
                };
                NonClaimsPrincipal nonClaimsPrincipal = new NonClaimsPrincipal()
                {
                    Identity = id
                };

                Thread.CurrentPrincipal = nonClaimsPrincipal;

                ClaimsPrincipal current = ClaimsPrincipal.Current;
                Assert.NotNull(current);
                Assert.Equal("NonClaimsIdentity_Name", current.Identity.Name);

                Thread.CurrentPrincipal = null;
                Assert.Null(ClaimsPrincipal.Current);
            }).Dispose();
        }
Exemplo n.º 2
0
        public void Ctor_IdentityEnumerableClaimAuthNameRoleType_NonClaimsIdentityClaims()
        {
            var baseId = new NonClaimsIdentity
            {
                Name = "base_name",
                AuthenticationType = "TestId_AuthType"
            };

            var id = new ClaimsIdentity(
                baseId,
                new[] {
                new Claim("claim_type", "claim_value"),
                new Claim(ClaimsIdentity.DefaultNameClaimType, "claim_name_value"),
                new Claim("claim_role_type", "claim_role_value"),
            });

            Assert.Equal("TestId_AuthType", id.AuthenticationType);

            Assert.Null(id.Actor);
            Assert.Null(id.BootstrapContext);
            Assert.NotNull(id.Claims);
            Assert.Equal(4, id.Claims.Count());
            Assert.Equal(2, id.Claims.Count(_ => _.Type == ClaimsIdentity.DefaultNameClaimType));
            Assert.True(id.IsAuthenticated);
            Assert.Null(id.Label);
            Assert.Equal("base_name", id.Name);
            Assert.Equal(ClaimsIdentity.DefaultNameClaimType, id.NameClaimType);
            Assert.Equal(ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType);
        }
        public void Current_FallsBackToThread()
        {
            RemoteExecutor.Invoke(() =>
            {
                ClaimsPrincipal principal1 = new ClaimsPrincipal();
                ClaimsPrincipal principal2 = new ClaimsPrincipal();

                Thread.CurrentPrincipal = principal1;
                Assert.Same(principal1, ClaimsPrincipal.Current);

                Thread.CurrentPrincipal = principal2;
                Assert.Same(principal2, ClaimsPrincipal.Current);

                NonClaimsIdentity id = new NonClaimsIdentity()
                {
                    Name = "NonClaimsIdentity_Name"
                };
                NonClaimsPrincipal nonClaimsPrincipal = new NonClaimsPrincipal()
                {
                    Identity = id
                };

                Thread.CurrentPrincipal = nonClaimsPrincipal;

                ClaimsPrincipal current = ClaimsPrincipal.Current;
                Assert.NotNull(current);
                Assert.Equal("NonClaimsIdentity_Name", current.Identity.Name);

                // match .NET Framework behavior by throwing ArgumentNullException when Thread.CurrentPrincipal is null
                Thread.CurrentPrincipal = null;
                Assert.Throws <ArgumentNullException>(() => ClaimsPrincipal.Current);
            }).Dispose();
        }
Exemplo n.º 4
0
        public void Ctor_IIdentity_NonClaims()
        {
            var id = new NonClaimsIdentity() { Name = "NonClaimsIdentity_Name" };
            var cp = new ClaimsPrincipal(id);

            Assert.NotNull(cp.Identities);
            Assert.Equal(1, cp.Identities.Count());

            Assert.NotSame(id, cp.Identities.First());
            Assert.NotSame(id, cp.Identity);
            Assert.Equal(id.Name, cp.Identity.Name);

            Assert.NotNull(cp.Claims);
            Assert.Equal(1, cp.Claims.Count());
            Assert.True(cp.Claims.Any(claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "NonClaimsIdentity_Name"));
        }
Exemplo n.º 5
0
        public void Ctor_NonClaimsIPrincipal_NonClaimsIdentity()
        {
            var id = new NonClaimsIdentity()
            {
                Name = "NonClaimsIdentity_Name"
            };
            var basePrincipal = new NonClaimsPrincipal {
                Identity = id
            };
            var cp = new ClaimsPrincipal(basePrincipal);

            Assert.NotNull(cp.Identities);
            Assert.Equal(1, cp.Identities.Count());

            Assert.NotSame(id, cp.Identities.First());
            Assert.NotSame(id, cp.Identity);
            Assert.Equal(id.Name, cp.Identity.Name);

            Assert.NotNull(cp.Claims);
            Assert.Equal(1, cp.Claims.Count());
            Assert.Contains(cp.Claims, claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "NonClaimsIdentity_Name");
        }