Example #1
0
 public async Task LinkUnlinkDeletesTest()
 {
     var db = UnitTestHelper.CreateDefaultDb();
     var mgr = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
     var user = new IdentityUser("linkunlinktest");
     UnitTestHelper.IsSuccess(await mgr.CreateAsync(user));
     var userLogin1 = new UserLoginInfo("provider1", "p1-1");
     var userLogin2 = new UserLoginInfo("provider2", "p2-1");
     Assert.Equal(0, (await mgr.GetLoginsAsync(user.Id)).Count);
     UnitTestHelper.IsSuccess(await mgr.AddLoginAsync(user.Id, userLogin1));
     Assert.Equal(1, user.Logins.Count(l => l.ProviderKey == "p1-1"));
     Assert.Equal(1, (await mgr.GetLoginsAsync(user.Id)).Count);
     UnitTestHelper.IsSuccess(await mgr.AddLoginAsync(user.Id, userLogin2));
     Assert.Equal(1, user.Logins.Count(l => l.ProviderKey == "p2-1"));
     Assert.Equal(2, (await mgr.GetLoginsAsync(user.Id)).Count);
     UnitTestHelper.IsSuccess(await mgr.RemoveLoginAsync(user.Id, userLogin1));
     Assert.Equal(0, user.Logins.Count(l => l.ProviderKey == "p1-1"));
     Assert.Equal(1, user.Logins.Count(l => l.ProviderKey == "p2-1"));
     Assert.Equal(1, (await mgr.GetLoginsAsync(user.Id)).Count());
     UnitTestHelper.IsSuccess(await mgr.RemoveLoginAsync(user.Id, userLogin2));
     Assert.Equal(0, (await mgr.GetLoginsAsync(user.Id)).Count);
     Assert.Equal(0, db.Set<IdentityUserLogin>().Count());
 }
Example #2
0
        public async Task<ActionResult> DeleteConfirmed(string id) {

            var roleStore = new RoleStore<IdentityRole>(db);
            var roleManager = new RoleManager<IdentityRole>(roleStore);

            var userStore = new UserStore<ApplicationUser>(db);
            var userManager = new UserManager<ApplicationUser>(userStore);

            if (ModelState.IsValid) {
                if (id == null) {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var user = await userManager.FindByIdAsync(id);
                
                // ev. 3parts inloggningar
                var logins = user.Logins;
                foreach (var login in logins.ToList()) {
                    await userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
                }

                var rolesForUser = await userManager.GetRolesAsync(id);

                if (rolesForUser.Count() > 0) {
                    foreach (var item in rolesForUser.ToList()) {
                        // item should be the name of the role
                        var result = await userManager.RemoveFromRoleAsync(user.Id, item);
                    }
                }

                if (user.Documents.Count() > 0) {
                    foreach (var doc in user.Documents.ToList()) {
                        db.Documents.Remove(doc);
                    }
                }

                await userManager.DeleteAsync(user);

                return RedirectToAction("Index");
            }
            else {
                return View();
            }
        }