예제 #1
0
        public async Task <IActionResult> Login([Bind("Id,Email,Password")] Login login)
        {
            if (ModelState.IsValid)
            {
                if (!ReCaptchaPassed(Request.Form["endForm"]))
                {
                    ModelState.AddModelError(string.Empty, "You failed the CAPTCHA.");
                    ViewBag.Error = "You failed the CAPTCHA. Please try again later.";
                    return(View());
                }

                // Get Account with Same email
                var account = db.Customer.Where(s => s.EmailAddress == login.Email).FirstOrDefault();

                // Valid Email
                if (account != null)
                {
                    string salt = account.PasswordSalt;

                    // Compare Hashed Passwords
                    byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(login.Password + salt);
                    byte[] hashBytes            = new SHA256Managed().ComputeHash(passwordAndSaltBytes);
                    string hashString           = Convert.ToBase64String(hashBytes);

                    // Correct Password
                    if (hashString == account.PasswordHash)
                    {
                        TempData["Login"] = "******";
                        db.Login.Add(login);
                        await db.SaveChangesAsync();

                        // Sessions
                        //https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1

                        var claims = new List <Claim>
                        {
                            new Claim("Email", login.Email),
                            new Claim("Name", account.FirstName + " " + account.LastName),
                            new Claim("Role", account.Privileges),
                        };

                        var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                        var authProperties = new AuthenticationProperties {
                        };

                        await HttpContext.SignInAsync(
                            CookieAuthenticationDefaults.AuthenticationScheme,
                            new ClaimsPrincipal(claimsIdentity),
                            authProperties);

                        return(RedirectToAction("Index"));
                    }
                }
            }


            TempData["Error"] = "Invalid login or password";
            return(RedirectToAction("SignIn", "Authentication"));
        }
        public async Task <IActionResult> Create([Bind("ProductCategoryId,ParentProductCategoryId,Name")] ProductCategory productCategory)
        {
            if (ModelState.IsValid)
            {
                productCategory.ModifiedDate = DateTime.Now;
                db.Add(productCategory);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ParentProductCategoryId"] = new SelectList(db.ProductCategory, "ProductCategoryId", "Name", productCategory.ParentProductCategoryId);
            return(View(productCategory));
        }