Esempio n. 1
0
 public ClaimsIdentity TryConvert(IIdentity identity)
 {
     if (ClaimsIdentityConverter.IsGrandfatheredIdentityType(identity))
     {
         return(null);
     }
     for (int i = 0; i < this._converters.Length; i++)
     {
         ClaimsIdentity claimsIdentity = this._converters[i](identity);
         if (claimsIdentity != null)
         {
             return(claimsIdentity);
         }
     }
     return(null);
 }
Esempio n. 2
0
        private static Func <IIdentity, ClaimsIdentity>[] GetDefaultConverters()
        {
            List <Func <IIdentity, ClaimsIdentity> > list = new List <Func <IIdentity, ClaimsIdentity> >();

            if (AppDomain.CurrentDomain.IsHomogenous && AppDomain.CurrentDomain.IsFullyTrusted)
            {
                Type type  = Type.GetType("Microsoft.IdentityModel.Claims.IClaimsIdentity, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                Type type2 = Type.GetType("Microsoft.IdentityModel.Claims.Claim, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                ClaimsIdentityConverter.AddToList(list, type, type2);
            }
            Module module = typeof(object).Module;
            Type   type3  = module.GetType("System.Security.Claims.ClaimsIdentity");
            Type   type4  = module.GetType("System.Security.Claims.Claim");

            ClaimsIdentityConverter.AddToList(list, type3, type4);
            return(list.ToArray());
        }
        public void ExtractClaimUid_NotAClaimsIdentity()
        {
            // Arrange
            Mock<IIdentity> mockIdentity = new Mock<IIdentity>();
            mockIdentity.Setup(o => o.IsAuthenticated).Returns(true);
            MockAntiForgeryConfig config = new MockAntiForgeryConfig();
            ClaimsIdentityConverter converter = new ClaimsIdentityConverter(new Func<IIdentity, ClaimsIdentity>[0]);

            ClaimUidExtractor extractor = new ClaimUidExtractor(
                config: config,
                claimsIdentityConverter: converter);

            // Act
            BinaryBlob retVal = extractor.ExtractClaimUid(mockIdentity.Object);

            // Assert
            Assert.Null(retVal);
        }
 internal ClaimUidExtractor(IAntiForgeryConfig config, ClaimsIdentityConverter claimsIdentityConverter)
 {
     _config = config;
     _claimsIdentityConverter = claimsIdentityConverter;
 }
        public void ExtractClaimUid_ClaimsIdentity()
        {
            // Arrange
            Mock<IIdentity> mockIdentity = new Mock<IIdentity>();
            mockIdentity.Setup(o => o.IsAuthenticated).Returns(true);
            MockAntiForgeryConfig config = new MockAntiForgeryConfig()
            {
                UniqueClaimTypeIdentifier = "unique-identifier"
            };
            ClaimsIdentityConverter converter = new ClaimsIdentityConverter(new Func<IIdentity, ClaimsIdentity>[] {
               identity =>
               {
                   Assert.Equal(mockIdentity.Object, identity);
                   MockClaimsIdentity claimsIdentity = new MockClaimsIdentity();
                   claimsIdentity.AddClaim("unique-identifier", "some-value");
                   return claimsIdentity;
               }
            });

            ClaimUidExtractor extractor = new ClaimUidExtractor(
                config: config,
                claimsIdentityConverter: converter);

            // Act
            BinaryBlob retVal = extractor.ExtractClaimUid(mockIdentity.Object);

            // Assert
            Assert.NotNull(retVal);
            Assert.Equal("CA9CCFF86F903FBB7505BAAA9F222E49EC2A1E8FAD630AE73DE180BD679751ED", HexUtil.HexEncode(retVal.GetData()));
        }