Ejemplo n.º 1
0
 public async Task CreateIdentityNullChecks()
 {
     var userManager = MockHelpers.MockUserManager<TestUser>().Object;
     var roleManager = MockHelpers.MockRoleManager<TestRole>().Object;
     var factory = new ClaimsIdentityFactory<TestUser, TestRole>(userManager, roleManager);
     await Assert.ThrowsAsync<ArgumentNullException>("user",
         async () => await factory.CreateAsync(null, new ClaimsIdentityOptions()));
     await Assert.ThrowsAsync<ArgumentNullException>("options",
         async () => await factory.CreateAsync(new TestUser(), null));
 }
Ejemplo n.º 2
0
        public async Task CreateIdentityNullChecks()
        {
            var userManager = MockHelpers.MockUserManager <TestUser>().Object;
            var roleManager = MockHelpers.MockRoleManager <TestRole>().Object;
            var factory     = new ClaimsIdentityFactory <TestUser, TestRole>(userManager, roleManager);
            await Assert.ThrowsAsync <ArgumentNullException>("user",
                                                             async() => await factory.CreateAsync(null, new ClaimsIdentityOptions()));

            await Assert.ThrowsAsync <ArgumentNullException>("options",
                                                             async() => await factory.CreateAsync(new TestUser(), null));
        }
 public void CreateIdentityNullCheckTest()
 {
     var factory = new ClaimsIdentityFactory<IdentityUser>();
     var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
     ExceptionHelper.ThrowsArgumentNull(
         () => AsyncHelper.RunSync(() => factory.CreateAsync(null, null, "whatever")), "manager");
     ExceptionHelper.ThrowsArgumentNull(
         () => AsyncHelper.RunSync(() => factory.CreateAsync(manager, null, "whatever")), "user");
     ExceptionHelper.ThrowsArgumentNull(
         () => AsyncHelper.RunSync(() => factory.CreateAsync(manager, new IdentityUser(), null)), "value");
     ExceptionHelper.ThrowsArgumentNull(() => factory.ConvertIdToString(null), "key");
 }
        public void CreateIdentityNullCheckTest()
        {
            var factory = new ClaimsIdentityFactory <IdentityUser>();
            var manager = new UserManager <IdentityUser>(new UserStore <IdentityUser>());

            ExceptionHelper.ThrowsArgumentNull(
                () => AsyncHelper.RunSync(() => factory.CreateAsync(null, null, "whatever")), "manager");
            ExceptionHelper.ThrowsArgumentNull(
                () => AsyncHelper.RunSync(() => factory.CreateAsync(manager, null, "whatever")), "user");
            ExceptionHelper.ThrowsArgumentNull(
                () => AsyncHelper.RunSync(() => factory.CreateAsync(manager, new IdentityUser(), null)), "value");
            ExceptionHelper.ThrowsArgumentNull(() => factory.ConvertIdToString(null), "key");
        }
Ejemplo n.º 5
0
 public Task <ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
 {
     //ThrowIfDisposed();
     if (user == null)
     {
         throw new ArgumentNullException("user");
     }
     return(ClaimsIdentityFactory.CreateAsync(this, user, authenticationType));
 }
        internal Task <System.Security.Claims.ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            TraceUtility.WriteTrace(this.GetType(), "GenerateUserIdentityAsync(ApplicationUserManager)", TraceUtility.TraceType.Begin);
            ClaimsIdentityFactory <ApplicationUser, string> factory = new ClaimsIdentityFactory <ApplicationUser, string>();
            ApplicationUser user = new ApplicationUser("BOGUS", "bougus1234");

            TraceUtility.WriteTrace(this.GetType(), "GenerateUserIdentityAsync(ApplicationUserManager)", TraceUtility.TraceType.End);
            return(factory.CreateAsync(manager, user, "Custom"));
        }
Ejemplo n.º 7
0
 public override Task <ClaimsIdentity> CreateIdentityAsync(COHApplicationUser user, string authenticationType)
 {
     if (user == null)
     {
         throw new ArgumentNullException("user");
     }
     else
     {
         return(ClaimsIdentityFactory.CreateAsync(this, user, authenticationType));
     }
 }
Ejemplo n.º 8
0
        public async Task CreateIdentityNullChecks()
        {
            var userManager = MockHelpers.MockUserManager <TestUser>().Object;
            var roleManager = MockHelpers.MockRoleManager <TestRole>().Object;
            var options     = new Mock <IOptions <IdentityOptions> >();

            Assert.Throws <ArgumentNullException>("optionsAccessor",
                                                  () => new ClaimsIdentityFactory <TestUser, TestRole>(userManager, roleManager, options.Object));
            var identityOptions = new IdentityOptions();

            options.Setup(a => a.Options).Returns(identityOptions);
            var factory = new ClaimsIdentityFactory <TestUser, TestRole>(userManager, roleManager, options.Object);
            await Assert.ThrowsAsync <ArgumentNullException>("user",
                                                             async() => await factory.CreateAsync(null));
        }
Ejemplo n.º 9
0
        public async Task EnsureClaimsIdentityHasExpectedClaims(bool supportRoles, bool supportClaims, bool supportRoleClaims)
        {
            // Setup
            var userManager = MockHelpers.MockUserManager <TestUser>();
            var roleManager = MockHelpers.MockRoleManager <TestRole>();
            var user        = new TestUser {
                UserName = "******"
            };

            userManager.Setup(m => m.SupportsUserClaim).Returns(supportClaims);
            userManager.Setup(m => m.SupportsUserRole).Returns(supportRoles);
            userManager.Setup(m => m.GetUserIdAsync(user, CancellationToken.None)).ReturnsAsync(user.Id);
            userManager.Setup(m => m.GetUserNameAsync(user, CancellationToken.None)).ReturnsAsync(user.UserName);
            var roleClaims = new[] { "Admin", "Local" };

            if (supportRoles)
            {
                userManager.Setup(m => m.GetRolesAsync(user, CancellationToken.None)).ReturnsAsync(roleClaims);
                roleManager.Setup(m => m.SupportsRoleClaims).Returns(supportRoleClaims);
            }
            var userClaims = new[] { new Claim("Whatever", "Value"), new Claim("Whatever2", "Value2") };

            if (supportClaims)
            {
                userManager.Setup(m => m.GetClaimsAsync(user, CancellationToken.None)).ReturnsAsync(userClaims);
            }
            userManager.Object.Options = new IdentityOptions();

            var admin = new TestRole()
            {
                Name = "Admin"
            };
            var local = new TestRole()
            {
                Name = "Local"
            };
            var adminClaims = new[] { new Claim("AdminClaim1", "Value1"), new Claim("AdminClaim2", "Value2") };
            var localClaims = new[] { new Claim("LocalClaim1", "Value1"), new Claim("LocalClaim2", "Value2") };

            if (supportRoleClaims)
            {
                roleManager.Setup(m => m.FindByNameAsync("Admin", CancellationToken.None)).ReturnsAsync(admin);
                roleManager.Setup(m => m.FindByNameAsync("Local", CancellationToken.None)).ReturnsAsync(local);
                roleManager.Setup(m => m.GetClaimsAsync(admin, CancellationToken.None)).ReturnsAsync(adminClaims);
                roleManager.Setup(m => m.GetClaimsAsync(local, CancellationToken.None)).ReturnsAsync(localClaims);
            }

            var factory = new ClaimsIdentityFactory <TestUser, TestRole>(userManager.Object, roleManager.Object);

            // Act
            var identity = await factory.CreateAsync(user, new ClaimsIdentityOptions());

            // Assert
            var manager = userManager.Object;

            Assert.NotNull(identity);
            Assert.Equal(ClaimsIdentityOptions.DefaultAuthenticationType, identity.AuthenticationType);
            var claims = identity.Claims.ToList();

            Assert.NotNull(claims);
            Assert.True(
                claims.Any(c => c.Type == manager.Options.ClaimsIdentity.UserNameClaimType && c.Value == user.UserName));
            Assert.True(claims.Any(c => c.Type == manager.Options.ClaimsIdentity.UserIdClaimType && c.Value == user.Id));
            Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Admin"));
            Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Local"));
            foreach (var cl in userClaims)
            {
                Assert.Equal(supportClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            foreach (var cl in adminClaims)
            {
                Assert.Equal(supportRoleClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            foreach (var cl in localClaims)
            {
                Assert.Equal(supportRoleClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            userManager.VerifyAll();
            roleManager.VerifyAll();
        }
Ejemplo n.º 10
0
        public async Task EnsureClaimsIdentityHasExpectedClaims(bool supportRoles, bool supportClaims, bool supportRoleClaims)
        {
            // Setup
            var userManager = MockHelpers.MockUserManager<TestUser>();
            var roleManager = MockHelpers.MockRoleManager<TestRole>();
            var user = new TestUser { UserName = "******" };
            userManager.Setup(m => m.SupportsUserClaim).Returns(supportClaims);
            userManager.Setup(m => m.SupportsUserRole).Returns(supportRoles);
            userManager.Setup(m => m.GetUserIdAsync(user, CancellationToken.None)).ReturnsAsync(user.Id);
            userManager.Setup(m => m.GetUserNameAsync(user, CancellationToken.None)).ReturnsAsync(user.UserName);
            var roleClaims = new[] { "Admin", "Local" };
            if (supportRoles)
            {
                userManager.Setup(m => m.GetRolesAsync(user, CancellationToken.None)).ReturnsAsync(roleClaims);
                roleManager.Setup(m => m.SupportsRoleClaims).Returns(supportRoleClaims);
            }
            var userClaims = new[] { new Claim("Whatever", "Value"), new Claim("Whatever2", "Value2") };
            if (supportClaims)
            {
                userManager.Setup(m => m.GetClaimsAsync(user, CancellationToken.None)).ReturnsAsync(userClaims);
            }
            userManager.Object.Options = new IdentityOptions();

            var admin = new TestRole() { Name = "Admin" };
            var local = new TestRole() { Name = "Local" };
            var adminClaims = new[] { new Claim("AdminClaim1", "Value1"), new Claim("AdminClaim2", "Value2") };
            var localClaims = new[] { new Claim("LocalClaim1", "Value1"), new Claim("LocalClaim2", "Value2") };
            if (supportRoleClaims)
            {
                roleManager.Setup(m => m.FindByNameAsync("Admin", CancellationToken.None)).ReturnsAsync(admin);
                roleManager.Setup(m => m.FindByNameAsync("Local", CancellationToken.None)).ReturnsAsync(local);
                roleManager.Setup(m => m.GetClaimsAsync(admin, CancellationToken.None)).ReturnsAsync(adminClaims);
                roleManager.Setup(m => m.GetClaimsAsync(local, CancellationToken.None)).ReturnsAsync(localClaims);
            }

            var factory = new ClaimsIdentityFactory<TestUser, TestRole>(userManager.Object, roleManager.Object);

            // Act
            var identity = await factory.CreateAsync(user, new ClaimsIdentityOptions());

            // Assert
            var manager = userManager.Object;
            Assert.NotNull(identity);
            Assert.Equal(ClaimsIdentityOptions.DefaultAuthenticationType, identity.AuthenticationType);
            var claims = identity.Claims.ToList();
            Assert.NotNull(claims);
            Assert.True(
                claims.Any(c => c.Type == manager.Options.ClaimsIdentity.UserNameClaimType && c.Value == user.UserName));
            Assert.True(claims.Any(c => c.Type == manager.Options.ClaimsIdentity.UserIdClaimType && c.Value == user.Id));
            Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Admin"));
            Assert.Equal(supportRoles, claims.Any(c => c.Type == manager.Options.ClaimsIdentity.RoleClaimType && c.Value == "Local"));
            foreach (var cl in userClaims)
            {
                Assert.Equal(supportClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            foreach (var cl in adminClaims)
            {
                Assert.Equal(supportRoleClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            foreach (var cl in localClaims)
            {
                Assert.Equal(supportRoleClaims, claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
            }
            userManager.VerifyAll();
            roleManager.VerifyAll();
        }