コード例 #1
0
 public async Task CreateItem(Product product)
 {
     _context.Products.Add(product);
     await _context.SaveChangesAsync();
 }
コード例 #2
0
        /// <summary>
        /// Registers a new user with a valid model state and signs that user in.
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns>Returns user to back to  the home page</returns>
        public async Task <IActionResult> OnPost(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = Input.Email,
                    Email       = Input.Email,
                    FirstName   = Input.FirstName,
                    LastName    = Input.LastName,
                    DOB         = Input.DOB,
                    PhoneNumber = Input.PhoneNumber
                };

                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    Claim fullNameClaim = new Claim("FullName", $"{user.FirstName} {user.LastName}");
                    Claim dobClaim      = new Claim(ClaimTypes.DateOfBirth, new DateTime(user.DOB.Year, user.DOB.Month, user.DOB.Day).ToString("u"), ClaimValueTypes.DateTime);
                    Claim emailClaim    = new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.Email);
                    Claim phoneClaim    = new Claim(ClaimTypes.MobilePhone, user.PhoneNumber, ClaimValueTypes.String);

                    List <Claim> userClaims = new List <Claim>
                    {
                        fullNameClaim,
                        dobClaim,
                        emailClaim,
                        phoneClaim
                    };

                    ClaimsIdentity claimsIdentity = new ClaimsIdentity();
                    claimsIdentity.AddClaims(userClaims);
                    ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal();
                    claimsPrincipal.AddIdentity(claimsIdentity);

                    await _userManager.AddClaimsAsync(user, userClaims);

                    if (user.Email.ToLower() == "*****@*****.**")
                    {
                        await _userManager.AddToRoleAsync(user, ApplicationRoles.Admin);
                    }

                    await _userManager.AddToRoleAsync(user, ApplicationRoles.Member);

                    await _emailSender.SendEmailAsync(user.Email, "Welcome To BeardsRUs", "<p>Thank you for registering to BeardsRUs</p>");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    var basket = new E_Commerce.Models.Basket
                    {
                        Email = user.Email
                    };
                    await _context.Baskets.AddAsync(basket);

                    await _context.SaveChangesAsync();

                    return(LocalRedirect(returnUrl));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }

            return(Page());
        }
コード例 #3
0
 public async Task CreateBasketItem(BasketItem basketItem)
 {
     _context.BasketItems.Add(basketItem);
     await _context.SaveChangesAsync();
 }