private async Task AuthorizeChildAndWishlistForFamilyUser(Child child, Wishlist wishlist) { var familyUser = await _db.Users.Where( u => u.Claims.Any(c => c.ClaimType == "Family" && c.ClaimValue == child.FamilyId.ToString()) ).FirstAsync(); using (var userStore = new UserStore<WishlistUser>(_db)) using (var userManager = new WishlistUserManager(userStore)) { await userManager.AddClaimAsync(familyUser.Id, new Claim("Child", child.Id.ToString())); await userManager.AddClaimAsync(familyUser.Id, new Claim("Wishlist", wishlist.Id.ToString())); if (User.Identity.GetUserId() == familyUser.Id) { var claimsIdenity = (ClaimsIdentity) User.Identity; claimsIdenity.AddClaim(new Claim("Child", child.Id.ToString())); claimsIdenity.AddClaim(new Claim("Wishlist", wishlist.Id.ToString())); HttpContext.GetOwinContext().Authentication.SignIn(claimsIdenity); } } }
public async Task<ActionResult> RegisterChild(int id, RegisterChildModel registration) { if (!ModelState.IsValid) { return View(registration); } var child = new Child { FamilyId = registration.FamilyId, FirstName = registration.FirstName, LastName = registration.LastName, Age = registration.Age, Gender = registration.Gender }; _db.Children.Add(child); var bio = new ChildBiography { Child = child, CreationDate = DateTime.Now, Text = registration.BiographyText }; _db.Biographies.Add(bio); var wishlist = new Wishlist { Child = child }; _db.WishLists.Add(wishlist); await _db.SaveChangesAsync(); await AuthorizeChildAndWishlistForFamilyUser(child, wishlist); TempData["registeredChild"] = new RegisteredChildViewModel { ChildId = child.Id, WishlistId = wishlist.Id, FirstName = child.FirstName }; return RedirectToAction("RegisterChild", new { id = registration.FamilyId }); }