public void ClaimsIdentitySyncTest()
 {
     var db = UnitTestHelper.CreateDefaultDb();
     var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
     var role = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
     var user = new IdentityUser("Hao");
     var claimsFactory = manager.ClaimsIdentityFactory as ClaimsIdentityFactory<IdentityUser, string>;
     Assert.NotNull(claimsFactory);
     UnitTestHelper.IsSuccess(manager.Create(user));
     UnitTestHelper.IsSuccess(role.Create(new IdentityRole("Admin")));
     UnitTestHelper.IsSuccess(role.Create(new IdentityRole("Local")));
     UnitTestHelper.IsSuccess(manager.AddToRole(user.Id, "Admin"));
     UnitTestHelper.IsSuccess(manager.AddToRole(user.Id, "Local"));
     Claim[] userClaims =
     {
         new Claim("Whatever", "Value"),
         new Claim("Whatever2", "Value2")
     };
     foreach (var c in userClaims)
     {
         UnitTestHelper.IsSuccess(manager.AddClaim(user.Id, c));
     }
     var identity = manager.CreateIdentity(user, "test");
     var claims = identity.Claims;
     Assert.NotNull(claims);
     Assert.NotNull(claims);
     Assert.True(
         claims.Any(c => c.Type == claimsFactory.UserNameClaimType && c.Value == user.UserName));
     Assert.True(claims.Any(c => c.Type == claimsFactory.UserIdClaimType && c.Value == user.Id));
     Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Admin"));
     Assert.True(claims.Any(c => c.Type == claimsFactory.RoleClaimType && c.Value == "Local"));
     Assert.True(
         claims.Any(
             c =>
                 c.Type == ClaimsIdentityFactory<IdentityUser>.IdentityProviderClaimType &&
                 c.Value == ClaimsIdentityFactory<IdentityUser>.DefaultIdentityProviderClaimValue));
     foreach (var cl in userClaims)
     {
         Assert.True(claims.Any(c => c.Type == cl.Type && c.Value == cl.Value));
     }
 }
Example #2
0
 public void GetUserClaimSyncTest()
 {
     var db = UnitTestHelper.CreateDefaultDb();
     var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
     var user = new IdentityUser("u1");
     var result = manager.Create(user);
     UnitTestHelper.IsSuccess(result);
     Assert.NotNull(user);
     var claims = new[]
     {
         new Claim("c1", "v1"),
         new Claim("c2", "v2"),
         new Claim("c3", "v3")
     };
     foreach (Claim c in claims)
     {
         UnitTestHelper.IsSuccess(manager.AddClaim(user.Id, c));
     }
     var userClaims = new List<Claim>(manager.GetClaims(user.Id));
     Assert.Equal(3, userClaims.Count);
     foreach (Claim c in claims)
     {
         Assert.True(userClaims.Exists(u => u.Type == c.Type && u.Value == c.Value));
     }
 }
 public ActionResult TeacherEdit(User input)
 {
     UserManager manager = new UserManager();
     User user = manager.findById(input.Id);
     if (ModelState.IsValid) {
         manager.AddClaim(input.Id, new Claim(ClaimTypes.Role, "teacher"));
         return RedirectToAction("Index");
     }
     return View(user);
 }
 public void CreateWithoutCommitingNHibernateTransactionShouldNotInsertRows()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     using (var ts = _session.BeginTransaction())
     {
         userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome1");
         var x = userManager.FindByEmail("[email protected]");
         roleManager.Create(new IdentityRole("Admin"));
         userManager.AddClaim(x.Id, new Claim("role", "admin"));
         userManager.AddClaim(x.Id, new Claim("role", "user"));
         userManager.AddToRole(x.Id, "Admin");
         userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     }
     var x2 = userManager.FindByEmail("[email protected]");
     Assert.IsNull(x2);
 }
 public void CreateWithoutCommitingTransactionScopeShouldNotInsertRows()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew))
     {
         // session is not opened inside the scope so we need to enlist it manually
         ((System.Data.Common.DbConnection)_session.Connection).EnlistTransaction(System.Transactions.Transaction.Current);
         userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome1");
         var x = userManager.FindByEmail("[email protected]");
         roleManager.Create(new IdentityRole("Admin"));
         userManager.AddClaim(x.Id, new Claim("role", "admin"));
         userManager.AddClaim(x.Id, new Claim("role", "user"));
         userManager.AddToRole(x.Id, "Admin");
         userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     }
     var x2 = userManager.FindByEmail("[email protected]");
     Assert.IsNull(x2);
 }
 public void FindByEmailAggregated()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this._session));
     userManager.Create(new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true }, "Welcome");
     var x = userManager.FindByEmail("[email protected]");
     roleManager.CreateAsync(new IdentityRole("Admin"));
     userManager.AddClaim(x.Id, new Claim("role", "admin"));
     userManager.AddClaim(x.Id, new Claim("role", "user"));
     userManager.AddToRole(x.Id, "Admin");
     userManager.AddLogin(x.Id, new UserLoginInfo("facebook", "1234"));
     this._session.Clear();
     x = userManager.FindByEmail("[email protected]");
     Assert.IsNotNull(x);
     Assert.AreEqual(2, x.Claims.Count);
     Assert.AreEqual(1, x.Roles.Count);
     Assert.AreEqual(1, x.Logins.Count);
 }
 public void AddClaim()
 {
     var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this._session));
     var user = new ApplicationUser() { UserName = "test", Email = "[email protected]", EmailConfirmed = true };
     userManager.Create(user, "Welcome");
     userManager.AddClaim(user.Id, new Claim(ClaimTypes.Role, "Admin"));
     Assert.AreEqual(1, userManager.GetClaims(user.Id).Count());
 }