Ejemplo 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();
        }
        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();
        }
Ejemplo n.º 3
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.True(cp.Claims.Any(claim => claim.Type == ClaimsIdentity.DefaultNameClaimType && claim.Value == "NonClaimsIdentity_Name"));
        }