Exemplo n.º 1
0
        public async Task <IActionResult> Login(Company login)
        {
            if (ModelState.IsValid)
            {
                // Gets A List Of Registered Adminstrators Within The Database
                IEnumerable <Administrator> allRegisteredAdministrators = companyAdmin.GetAllReferences;
                // Loop Through All Registered Administrators
                foreach (var registeredAdministrators in allRegisteredAdministrators)
                {
                    // Check To See If Login Credentials Are Verified
                    IsAdministratorPasswordValid = registeredAdministrators.Password.Equals(login.Administrator.Password);
                    IsAdministratorEmailValid    = registeredAdministrators.Email.Equals(login.Administrator.Email);

                    // Checks To See If [INPUT] Password & Email Is Verified
                    if (IsAdministratorPasswordValid && IsAdministratorEmailValid)
                    {
                        // Adding A Cookie Authentication For Our Logged In Admin
                        UserAuthentication companyAuthentication = new UserAuthentication();
                        companyAuthentication.CreateClaim(registeredAdministrators);
                        companyAuthentication.CreateIdentity();


                        // Creating A Cookie Options
                        CookieOptions cookieOptions = new CookieOptions {
                            // Set Expire Time
                            Expires     = DateTime.Now.AddMinutes(5),
                            IsEssential = true,
                        };


                        // Convert The Object Data Into A [String]
                        string admin = JsonConvert.SerializeObject(registeredAdministrators);

                        // Append The Serialized Data Into The Cookie
                        HttpContext.Response.Cookies.Append(COOKIE_KEY, admin, cookieOptions);


                        // Adds Sign In Async Cookie Authentication
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                      companyAuthentication.GetClaimPrinciple());

                        // Redirects To The Dashboard
                        return(RedirectToAction("Dashboard", "Portal"));
                    }
                }
                return(View());
            }
            return(View());
        }