Ejemplo n.º 1
0
        public async Task <ActionResult> AdminLogin(Login login)
        {
            try
            {
                string sendEmail    = Crypto.Encrypt(login.Email, passPhrase);
                string sendPassword = Crypto.Encrypt(login.Password, passPhrase);
                Login  logSend      = new Login()
                {
                    Email    = sendEmail,
                    Password = sendPassword
                };

                HttpResponseMessage response = await client.PostAsJsonAsync(
                    "https://localhost:44389/api/1.0/adminlogin", logSend);

                Console.Write(response.IsSuccessStatusCode);
                response.EnsureSuccessStatusCode();
                var tempURL = response.Headers.Location;

                Console.WriteLine(tempURL);
                Admin tempAdmin = await GetAdminAsync(tempURL.ToString());

                string   id    = tempAdmin.Id;
                string   email = Crypto.Decrypt(tempAdmin.Email, passPhrase);
                OutLogin final = new OutLogin()
                {
                    Email = email,
                    Id    = id
                };
                HttpContext.Session.SetString("_Admin", "true");
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                string message    = e.Message;
                string stackTrace = e.StackTrace;
                HttpContext.Session.SetString("_Error", "true");
                HttpContext.Session.SetString("_ErrorMessage", message);
                HttpContext.Session.SetString("_ErrorTrace", stackTrace);
                return(View("AdminLoginError"));
            }
            //OutLogin temp = response.Content.ReadAsAsync<OutLogin>();
        }
Ejemplo n.º 2
0
        public string GenerateToken(OutLogin user)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(keySecret.Value.Key);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Login.ToString()),
                    new Claim(ClaimTypes.Role, user.Permissoes.ToString())
                }),
                Expires            = DateTime.UtcNow.AddHours(2),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> LoginUser(Login login)
        {
            try
            {
                string sendEmail    = Crypto.Encrypt(login.Email, passPhrase);
                string sendPassword = Crypto.Encrypt(login.Password, passPhrase);
                Login  logSend      = new Login()
                {
                    Email    = sendEmail,
                    Password = sendPassword
                };

                HttpResponseMessage response = await client.PostAsJsonAsync(
                    "https://localhost:44389/api/1.0/login", logSend);

                response.EnsureSuccessStatusCode();
                var tempURL = response.Headers.Location;
                Console.WriteLine(tempURL);
                User tempUser = await GetUserAsync(tempURL.ToString());

                string   id           = tempUser.Id;
                string   email        = Crypto.Decrypt(tempUser.Email, passPhrase);
                string   firstname    = Crypto.Decrypt(tempUser.FirstName, passPhrase);
                string   lastname     = Crypto.Decrypt(tempUser.LastName, passPhrase);
                string   address      = Crypto.Decrypt(tempUser.Address, passPhrase);
                string   city         = Crypto.Decrypt(tempUser.City, passPhrase);
                string   dob          = Crypto.Decrypt(tempUser.DOB, passPhrase);
                string   postCode     = Crypto.Decrypt(tempUser.PostCode, passPhrase);
                string   team         = Crypto.Decrypt(tempUser.Team, passPhrase);
                string   points       = Crypto.Decrypt(tempUser.Points, passPhrase);
                string   phoneNumber  = Crypto.Decrypt(tempUser.PhoneNumber, passPhrase);
                string   mobileNumber = Crypto.Decrypt(tempUser.MobilePhoneNumber, passPhrase);
                string   posistion    = Crypto.Decrypt(tempUser.Posistion, passPhrase);
                string   password     = Crypto.Decrypt(tempUser.Password, passPhrase);
                OutLogin final        = new OutLogin()
                {
                    Email = email,
                    Id    = id
                };
                HttpContext.Session.SetString("_Name", firstname);
                HttpContext.Session.SetString("_ID", id);
                HttpContext.Session.SetString("_Email", email);
                HttpContext.Session.SetString("_LoggedIn", "true");
                HttpContext.Session.SetString("_LastName", lastname);
                HttpContext.Session.SetString("_Address", address);
                HttpContext.Session.SetString("_PostCode", postCode);
                HttpContext.Session.SetString("_City", city);
                HttpContext.Session.SetString("_DOB", dob);
                HttpContext.Session.SetString("_Team", team);
                HttpContext.Session.SetString("_Points", points);
                HttpContext.Session.SetString("_PhoneNumber", phoneNumber);
                HttpContext.Session.SetString("_MobileNumber", mobileNumber);
                HttpContext.Session.SetString("_Posistion", posistion);
                HttpContext.Session.SetString("_Password", password);
                return(RedirectToAction("Index"));
                //OutLogin temp = response.Content.ReadAsAsync<OutLogin>();
            }
            catch (Exception e)
            {
                string message    = e.Message;
                string stackTrace = e.StackTrace;
                HttpContext.Session.SetString("_Error", "true");
                HttpContext.Session.SetString("_ErrorMessage", message);
                HttpContext.Session.SetString("_ErrorTrace", stackTrace);
                return(View("UserLoginFail"));
            }
        }