Example #1
0
        public async Task<ActionResult> Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            var authProxy = new AuthenticationProxy(WebConfigurationManager.AppSettings["WebApiUrl"], "/api/oauth");

            var token = await authProxy.Login(model.UserName, model.Password);
            if (token == null)
            {
                ModelState.AddModelError("password", "Wachtwoord of gebruikersnaam is onjuist");
                return View();
            }

            var tokenCookie = new HttpCookie("token", token.Value)
            {
                Expires = DateTime.Now.AddMinutes(token.ExpiresIn),
                HttpOnly = false
            };

            var roleCookie = new HttpCookie("role", token.Role)
            {
                Expires = DateTime.Now.AddMinutes(token.ExpiresIn),
                HttpOnly = true
            };

            Response.Cookies.Add(tokenCookie);
            Response.Cookies.Add(roleCookie);
            
            return RedirectToAction("Index", "Dashboard");
        }