[ValidateAntiForgeryToken]        // Ensures that the form that send the request is the right one
        public async Task <IActionResult> SignIn(SignInViewModel model)
        {
            //The user role initializeation.
            string role = model.SignInUserRole.Equals("0") ? "Organizer" : (model.SignInUserRole.Equals("1") ? "Spanner" : null);

            if (role.Equals("Spanner"))
            {
                ModelState.Remove("OrganizerGroupId");                 // Remove all validation of OrganizerGroupId if the role is Spanner
            }

            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction(nameof(HomeController.Index)));
            }
            else if (String.IsNullOrEmpty(role))
            {
                ViewBag.error = "Select your Role first";
                return(RedirectToAction(nameof(HomeController.Index)));
            }
            else if (String.IsNullOrEmpty(model.Code))
            {
                ViewBag.error = "Please Enter your Code first";
                return(RedirectToAction(nameof(HomeController.Index)));
            }

            /*else if ( role.Equals("Organizer") && String.IsNullOrEmpty(model.OrganizerGroupId) )
             * {
             *      model.OrganizerGroups = _flightSpannersData.GetOrganizerGroupSelectListItems(model.Code);
             *      return View(nameof(HomeController.SignIn), model);
             * }*/

            if (ModelState.IsValid)
            {
                //groupSelectedIndex the value of selected group SelectListItem
                if (String.IsNullOrEmpty(model.OrganizerGroupId) && role.Equals("Organizer"))
                {
                    ViewBag.error         = "Please Select Group";
                    model.OrganizerGroups = _flightSpannersData.GetOrganizerGroupSelectListItems(model.Code);
                    return(View(nameof(HomeController.SignIn), model));
                }

                bool isCodeOk = false, isPasswordOk = false;                //, isPasswordOrganizerOk = false, isPasswordSpannerOk = false;

                //Check the existance of the entered code
                isCodeOk = role.Equals("Organizer") ? _flightSpannersData.ValidateOrganizerCode(model.Code)
                                                                                                                                                                                : _flightSpannersData.ValidateSpannerCode(model.Code);

                if (isCodeOk)                //Check the password only if code exists.
                {
                    //Validate the password with the code as organizer and/or spanner
                    isPasswordOk = role.Equals("Organizer") ? _flightSpannersData.ValidatePasswordOrganizer(model.Code, model.Password)
                                                                                                                                                                                                  : _flightSpannersData.ValidatePasswordSpanner(model.Code, model.Password);
                }

                //string actor = (isPasswordOrganizerOk && isPasswordSpannerOk) ? "double" : "single";

                string groupName = role.Equals("Organizer") ? _flightSpannersData.GetOrganizerGroupSelectListItems(model.Code)[Convert.ToInt32(model.OrganizerGroupId)].Text
                                                                                                                                                                                                                : _flightSpannersData.GetSpannerGroup(model.Code);
                if (isPasswordOk)
                {
                    //Call the local AuthenticationCookieBasedAsync() method async to authinticate unauthenticated user.
                    await AuthenticationCookieBasedAsync(model.Code, role, groupName);
                }
                else if (!isPasswordOk)
                {
                    //Transfer the error message of wrong password to the view
                    //To pass error value from controller to view, ViewBag's life only lasts during current http request.
                    ViewBag.error         = "The Password is wrong, Please try again!";
                    model.OrganizerGroups = _flightSpannersData.GetOrganizerGroupSelectListItems(model.Code);
                    return(View(nameof(HomeController.SignIn), model));
                }
                else if (!isCodeOk)
                {
                    //Transfer the error message of wrong code to the view
                    //To pass error value from controller to view, ViewBag's life only lasts during current http request.
                    ViewBag.error = "This Code is not registered";
                    return(View(nameof(HomeController.Index)));
                }
            }
            else if (!ModelState.IsValid)
            {
                model.OrganizerGroups = _flightSpannersData.GetOrganizerGroupSelectListItems(model.Code);
                return(View(nameof(HomeController.SignIn), model));
            }

            //role = role.Equals(null) ? this.User.FindFirst(ClaimTypes.Role).Value : role;

            //The redirect sequence first to returnUrl, then to index of organizer PersonalData controller
            // ,  then to index of spanner PersonalData controller
            //&& !returnUrl.ToLower().EndsWith("signout")
            if ((model.ReturnUrl != null) && Url.IsLocalUrl(model.ReturnUrl))              // To protecting against open redirect attacks
            {
                return(LocalRedirect(model.ReturnUrl));
            }
            else if (role.Equals("Organizer"))
            {
                return(RedirectToAction(nameof(PersonalDataController.Index), _flightSpannersData.ControllerName(nameof(PersonalDataController)), new { area = nameof(OrganizerArea) }));
                //Equivelant to RedirectToAction("Index", "/OrganizerArea/PersonalData");
                // , code = model.OrganizerCode
            }
            else if (role.Equals("Spanner"))
            {
                return(RedirectToAction(nameof(PersonalDataController.Index), _flightSpannersData.ControllerName(nameof(PersonalDataController)), new { area = nameof(SpannerArea) }));
                //return RedirectToAction( "Index", "PersonalData", new { area = "SpannerArea" } );
                //return Content("Spanner Personal Data");
            }

            // If all the above redirects to the views not work then redirects to the index view of home controller.
            return(View(nameof(HomeController.Index)));
        }