Example #1
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                string password = model.Password;

                // generate a 128-bit salt using a secure PRNG
                string a = "Соль";

                byte[] salt = Encoding.Default.GetBytes(a);

                // derive a 256-bit subkey (use HMACSHA1 with 10,000 iterations)
                string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                           password: password,
                                                           salt: salt,
                                                           prf: KeyDerivationPrf.HMACSHA1,
                                                           iterationCount: 10000,
                                                           numBytesRequested: 256 / 8));
                string remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();

                user user = await db.User.FirstOrDefaultAsync(u => u.login == model.Email && u.pass == hashed);

                if (user != null)
                {
                    auth_date pole = new auth_date();

                    pole.login           = user.login;
                    pole.date            = DateTime.Now;
                    pole.autorizing      = 1;
                    pole.ip              = remoteIpAddress;
                    db.Entry(pole).State = EntityState.Added;

                    db.SaveChanges();

                    await Authenticate(model.Email); // аутентификация

                    return(RedirectToAction("Lk", "Lk", new { id = user.id }));
                }
                else
                {
                    auth_date pole = new auth_date();
                    pole.login           = model.Email;
                    pole.date            = DateTime.Now;
                    pole.autorizing      = 0;
                    pole.ip              = remoteIpAddress;
                    db.Entry(pole).State = EntityState.Added;
                    db.SaveChanges();
                }

                ModelState.AddModelError("", "Некорректные логин и(или) пароль");
            }

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Logout()
        {
            string    remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            auth_date pole            = new auth_date();

            pole.login           = HttpContext.User.Identity.Name;
            pole.date            = DateTime.Now;
            pole.autorizing      = 2;
            pole.ip              = remoteIpAddress;
            db.Entry(pole).State = EntityState.Added;

            db.SaveChanges();

            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            return(RedirectToAction("Index", "Home"));
        }