示例#1
0
        private async Task <bool> UserLogin(string email, string pass, User usr)
        {
            if (usr != null)
            {
                var bytepass = await CustomEncryptor.EncryptAsync(pass);

                var test    = System.Text.Encoding.UTF8.GetString(bytepass);
                var usrpass = System.Text.Encoding.UTF8.GetString(usr.Password);
                if (usrpass == test)
                {
                    GlobalDiagnosticsContext.Set("Email", usr.Email);

                    Session["Username"] = usr.Username;
                    Session["User"]     = usr;
                    logger.Info("Successful login, username:"******"Failed attempt to login:"******"Failed attempt to login:" + email);
                return(false);
            }
        }
示例#2
0
 private async Task <bool> NewUser(UserDTO user)
 {
     using (var set = new ForumContext())
     {
         var list = set.Users.Select(p => p.Email).Concat(set.Users.Select(p => p.Username));
         if (!ModelState.IsValid || list.Contains(user.Email) || list.Contains(user.Username))
         {
             if (list.Contains(user.Email))
             {
                 logger.Info("Failed attempt to register a registered user:"******"Email", "Such email is already registered");
             }
             else if (list.Contains(user.Username))
             {
                 logger.Info("Failed attempt to register a new user:"******"Username", "Such username is already registered");
             }
             else
             {
                 logger.Info(ModelState
                             .Values
                             .FirstOrDefault(e => e.Errors.Count != 0)
                             .Errors.FirstOrDefault()
                             .ErrorMessage);
             }
             return(false);
         }
         else
         {
             User usr = new User
             {
                 Name     = user.Name,
                 LastName = user.LastName,
                 Email    = user.Email,
                 Username = user.Username,
                 Password = await CustomEncryptor.EncryptAsync(user.Password),
                 City     = user.City
             };
             set.Users.Add(usr);
             set.SaveChanges();
             logger.Info("Registered a new user:" + user.Email);
             return(true);
         }
     }
 }
示例#3
0
        public async Task <ActionResult> EditUser(UserDTO user)
        {
            using (var set = new ForumContext())
            {
                var result = set.Users.FirstOrDefault(x => x.Id == user.Id);

                result.Password = await CustomEncryptor.EncryptAsync(user.Password);

                result.Email    = user.Email;
                result.Username = user.Username;


                await set.SaveChangesAsync();

                if (((User)Session["User"]).IsAdmin)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }
        }