Example #1
0
        public async Task <IActionResult> Login(string returnUrl, [FromForm] HomeworkAWPAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Check the sent data
                var result = await HealthAccountService.AuthenticateUser(model.Username, model.Password);

                if (result != null)
                {
                    //Create the claims that will be stored in the cookie
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, result.Username),
                        new Claim(ClaimTypes.Role, result.Role.Name),
                        new Claim(ClaimTypes.NameIdentifier, result.Id.ToString()),
                    };
                    //Create the claim identity
                    var claimIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    //Authorize the user and issue a cookie
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimIdentity));

                    return(Redirect(returnUrl ?? "/homework/awp"));
                }
                //Add the error message to the model
                model.Errors.Add(ErrorMessages.InvaildLoginAttempt);
            }
            else
            {
                model.Errors.AddRange(ModelState.GetValidationErrors());
            }
            //Reset captch value
            model.Captcha.CaptchaCode = string.Empty;

            return(View(StaticViewNames.AWP_HEALTH, model));
        }
Example #2
0
        public async Task <IActionResult> AWP()
        {
            var viewModel = new HomeworkAWPAccountViewModel();

            //Check if the user is authenitcated
            if (User.Identity.IsAuthenticated)
            {
                //Try to parse the user id
                if (Guid.TryParse(User.GetClaimValue(ClaimTypes.NameIdentifier), out Guid id))
                {
                    //Get the hole user information
                    var user = await HealthAccountService.GetUser(id);

                    //Fill up the values for the view model
                    viewModel.UserViewModel = new HealthUserViewModel()
                    {
                        DOB            = user.DOB,
                        Email          = user.Email,
                        Gender         = user.Gender,
                        Id             = user.Id,
                        MedicalHistory = user.MedicalHistory,
                        PhoneNumber    = user.PhoneNumber,
                        Username       = user.Username,
                    };
                }
            }
            return(View(StaticViewNames.AWP_HEALTH, viewModel));
        }