コード例 #1
0
        public void EncodeToDecode()
        {
            Cryptographer a = new Cryptographer();

            string input = "Привет, меня зовут Антон! А как зовут тебя? Hello, my name is <Anton> 1..";

            Assert.AreEqual(input, a.Decode(a.Encode(input)), true);
        }
コード例 #2
0
        public void EncodeWithStepOne()
        {
            Cryptographer a = new Cryptographer(1);

            string input    = "АБВГД";
            string expected = "БВГДЕ";

            string actual = a.Encode(input);

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public async Task <IActionResult> ProfileEdit([Bind("Id,Name, LastName, Email, DateRegister")] UserViewModel userViewModel)
        {
            long iduser;

            try
            {
                iduser = GetUserId();
            }
            catch
            {
                return(RedirectToAction("Signin", "Account"));
            }
            if (ModelState.IsValid)
            {
                UserApp userApp = null;
                UserApp temp    = null;
                try
                {
                    await Task.Run(() => {
                        userApp = new UserUtils().GetUserById(_context, iduser);
                    });
                }
                catch (Exception)
                {
                    return(NotFound());
                }
                if (userApp != null)
                {
                    Cryptographer cryptographer = new Cryptographer().Create(userApp.Upassword);
                    string        Email         = cryptographer.Encode(userViewModel.Email.Trim().ToLower());

                    try
                    {
                        temp = await _context.UserApps.SingleOrDefaultAsync(u => u.Email == Email);
                    }
                    catch (Exception)
                    {
                        return(NotFound());
                    }

                    if (userApp != null && temp != null)
                    {
                        if (userApp.Email == temp.Email)
                        {
                            userApp.FirstName = cryptographer.Encode(userViewModel.Name.Trim().ToLower());
                            userApp.LastName  = cryptographer.Encode(userViewModel.LastName.Trim().ToLower());

                            try
                            {
                                _context.Update(userApp);
                                await _context.SaveChangesAsync();
                            }
                            catch (DbUpdateConcurrencyException)
                            {
                                if (!UserAppExists(userApp.Id))
                                {
                                    return(NotFound());
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            return(RedirectToAction(nameof(ProfileEdit)));
                        }
                    }
                }
            }
            return(View(userViewModel));
        }
コード例 #4
0
        public async Task <IActionResult> Signin(LoginForm loginForm) // Sign in
        {
            HttpContext.Session.Remove(ERROR);

            if (ModelState.IsValid)
            {
                //TO DO
                // Send email and login to Java server
                // if exists in Java server  => select from MS SQL DATABASE
                // if not exists in MS SQL => save new UserApp
                //else = > redirect to Register form

                SomeData someData = null;

                try
                {
                    string someEmail = new HashConvertor().GetHash(loginForm.Email.Trim().ToLower());
                    someData = await _context.SomeDatas.LastOrDefaultAsync(sd => sd.Data1 == someEmail);
                }
                catch (Exception)
                {
                    return(NotFound());
                }

                if (someData == null)
                {
                    return(RedirectToAction("Register", "Account"));
                }

                Cryptographer cryptographer = new Cryptographer().Create(someData.Data2);

                UserApp userApp = null;

                try
                {
                    await Task.Run(() =>
                    {
                        string email    = cryptographer.Encode(loginForm.Email.Trim().ToLower());
                        string password = new HashConvertor().GetHash(loginForm.Password.Trim().ToLower());

                        userApp = new UserUtils().GetUser(_context, email, password);
                    });
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (userApp != null)
                {
                    if (userApp.EmailConfirmed)
                    {
                        int role;

                        try
                        {
                            role = _context.UserRoles.Where(r => r.Id == userApp.IdRole).Select(r => r.IdRole).First();
                        }
                        catch (Exception)
                        {
                            HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                            return(RedirectToAction("Error", "Account"));
                        }
                        SetUserSession(userApp, role, cryptographer);

                        if (role == 2)
                        {
                            return(RedirectToAction("Index", "Admin"));
                        }


                        return(RedirectToAction("Index", "BoardTasks"));
                    }
                    else
                    {
                        return(RedirectToAction("Confirm", "Account", new { id = userApp.Id }));
                    }
                }

                return(RedirectToAction("Register", "Account"));
            }

            return(View());
        }
コード例 #5
0
        public async Task <IActionResult> ChangePassword(ChangePassword changePassword) // Change password
        {
            if (ModelState.IsValid)
            {
                SomeData someData = null;

                try
                {
                    string someEmail = new HashConvertor().GetHash(changePassword.Email.Trim().ToLower());
                    someData = await _context.SomeDatas.LastOrDefaultAsync(sd => sd.Data1 == someEmail);
                }
                catch (Exception)
                {
                    return(NotFound());
                }

                if (someData == null)
                {
                    return(RedirectToAction("Register", "Account"));
                }

                Cryptographer cryptographer = new Cryptographer().Create(someData.Data2);

                string email = cryptographer.Encode(changePassword.Email.Trim().ToLower());

                string passwordHash = new HashConvertor().GetHash(changePassword.Password.Trim().ToLower());

                bool responce = false;

                try
                {
                    await Task.Run(() =>
                    {
                        responce = new UserUtils().CheckEmailExists(_context, email);
                    });
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (!responce)
                {
                    return(RedirectToAction("Register", "Account"));
                }

                UserApp userApp = null;
                try
                {
                    userApp = await _context.UserApps.SingleOrDefaultAsync(u => u.Email == email);

                    if (userApp != null)
                    {
                        userApp = cryptographer.ConvertUserData(userApp, passwordHash);

                        try
                        {
                            _context.Update(userApp);
                            await _context.SaveChangesAsync();
                        }
                        catch (Exception)
                        {
                            return(NotFound());
                        }

                        await Task.Run(() =>
                        {
                            userApp = new UserUtils().UpdatePassword(_context, userApp.Email, passwordHash);
                        });

                        string emailHash = new HashConvertor().GetHash(changePassword.Email.Trim().ToLower());

                        _context.Add(new SomeData
                        {
                            Data1 = emailHash,
                            Data2 = passwordHash
                        });
                        await _context.SaveChangesAsync();
                    }
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (userApp != null)
                {
                    int role;

                    try
                    {
                        role = _context.UserRoles.Where(r => r.Id == userApp.IdRole).Select(r => r.IdRole).First();
                    }
                    catch (Exception)
                    {
                        HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later. I am ");
                        return(RedirectToAction("Error", "Account"));
                    }

                    SetUserSession(userApp, role, cryptographer);

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

            return(View());
        }
コード例 #6
0
        public async Task <IActionResult> Register([Bind("Name,LastName,Email,Password,Confirm")] RegisterForm registerForm, string date_register) // Register
        {
            HttpContext.Session.Remove(ERROR);

            if (ModelState.IsValid)
            {
                string passwordHash = new HashConvertor().GetHash(registerForm.Password.Trim().ToLower());
                string emailHash    = new HashConvertor().GetHash(registerForm.Email.Trim().ToLower());

                SomeData someData = null;

                try
                {
                    someData = await _context.SomeDatas.LastOrDefaultAsync(sd => sd.Data1 == emailHash);
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (someData != null)
                {
                    return(RedirectToAction("Signin", "Account"));
                }

                Cryptographer cryptographer = new Cryptographer().Create(passwordHash);

                string emailEncode = cryptographer.Encode(registerForm.Email.Trim().ToLower());

                UserApp userApp = null;

                try
                {
                    await Task.Run(() =>
                    {
                        userApp = new UserUtils().GetUser(_context, emailEncode, passwordHash);
                    });
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("RegisterError", "Account"));
                }

                if (userApp != null)
                {
                    return(RedirectToAction("Signin", "Account"));
                }
                else
                {
                    EmailResponce goodEmail = await new EmailService().ChekEmaileService(registerForm.Email.Trim().ToLower());

                    switch (goodEmail.Success)
                    {
                    case 1:

                        string name     = cryptographer.Encode(registerForm.Name.Trim().ToLower());
                        string lastname = cryptographer.Encode(registerForm.LastName.Trim().ToLower());
                        string email    = emailEncode;
                        string password = passwordHash;



                        double localDate = Convert.ToDouble(date_register);
                        registerForm.Time = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(localDate);


                        try
                        {
                            _context.Add(new SomeData {
                                Data1 = emailHash,
                                Data2 = passwordHash
                            });
                            await _context.SaveChangesAsync();

                            await Task.Run(() =>
                            {
                                userApp = new UserUtils().RegisterNewUser(_context, name, lastname, email, password, registerForm.Time);
                            });
                        }
                        catch (Exception)
                        {
                            HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                            return(RedirectToAction("RegisterError", "Account"));
                        }

                        return(RedirectToAction("Confirm", "Account", new { id = userApp.Id }));

                    case -1:
                        HttpContext.Session.SetString(ERROR, "Mail is not correct.");
                        return(RedirectToAction("RegisterError", "Account"));
                    }
                }
            }
            return(View());
        }