Esempio n. 1
0
#pragma warning restore 1998

        public async Task <SessionViewModel> Login(SecureString password)
        {
            var ip = new WebClient().DownloadStringTaskAsync("http://icanhazip.com");


            var userList = await LoginViewModel.Where(x => UserName == x.UserName);

            if (userList.Count() == 0)
            {
                return(null);
            }
            var    user = userList.First();
            string hash = PasswordUtils.ComputePassword(password, user.Salt);

            if (hash != user.PasswordHash)
            {
                return(null);
            }
            var session = await SessionViewModel.Create();

            session.Time      = DateTime.Now;
            session.UserLogin = user;
            session.IP        = await ip;

            var student = (await StudentViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (student != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Student;
                session.User     = student;
                await session.Insert();

                return(session);
            }
            var lecturer = (await LecturerViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (lecturer != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Lecturer;
                session.User     = lecturer;
                await session.Insert();

                return(session);
            }

            var administrator = (await AdministratorViewModel.Where(x => x.LoginId == user.Id)).FirstOrDefault();

            if (administrator != null)
            {
                session.UserType = SessionViewModel.UserTypeEnum.Administrator;
                session.User     = administrator;
                await session.Insert();

                return(session);
            }
            throw new NotSupportedException();
        }
Esempio n. 2
0
        private async Task <LoginViewModel> RegisterUserLogin()
        {
            var userLogin = await LoginViewModel.Create();

            userLogin.UserName = UserName;
            userLogin.Salt     = PasswordUtils.CreateSalt();

            userLogin.PasswordHash = PasswordUtils.ComputePassword(Password, userLogin.Salt);
            await userLogin.Insert();

            return(userLogin);
        }