示例#1
0
        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username");
            }
            if (string.IsNullOrEmpty(oldPassword))
            {
                throw new ArgumentNullException("oldPassword");
            }
            if (string.IsNullOrEmpty(newPassword))
            {
                throw new ArgumentNullException("newPassword");
            }

            long userId = -1;
            using (var db = new LocalNewsDBEntities())
            {
                User user = db.Users.Where(u => u.username == username).FirstOrDefault();
                if (null != user && user.password == CryptoUtil.ComputeHash(oldPassword))
                {
                    userId = user.user_id;
                    user.password = CryptoUtil.ComputeHash(newPassword);
                    db.SaveChanges();
                }
            }
            if (userId > 0)
            {
                loginHistoryBll.Add(userId, LocalNewsConstant.User_Status_Password_Reset);
                return true;
            }
            else
                return false;
        }
示例#2
0
 public void Add(long userId, string userStatus)
 {
     Login_History history = new Login_History();
     history.user_id = userId;
     history.date_time = System.DateTime.Now;
     using (var db = new LocalNewsDBEntities())
     {
         User_Status status = db.User_Status.Where(us => us.status_name == userStatus).FirstOrDefault();
         history.status_id = status.status_id;
         db.Login_History.Add(history);
         db.SaveChanges();
     }
 }
示例#3
0
        public User Register(RegisterModel register)
        {
            if (register == null)
            {
                throw new ArgumentNullException("user");
            }

            User user = new User();
            user.first_name = register.FirstName;
            user.last_name = register.LastName;
            user.username = register.UserName;
            user.password = CryptoUtil.ComputeHash(register.Password);

            using (var db = new LocalNewsDBEntities())
            {
                db.Users.Add(user);
                db.SaveChanges();
            }
            loginHistoryBll.Add(user.user_id, LocalNewsConstant.User_Status_New_User);
            return user;
        }
示例#4
0
        public bool Update(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            using (var db = new LocalNewsDBEntities())
            {
                var userToUpdate = (from u in db.Users
                                    where u.username == user.username
                                    select u).FirstOrDefault();

                userToUpdate.username = user.username;
                int num = db.SaveChanges();
            }
            return true;
        }
示例#5
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (LocalNewsDBEntities db = new LocalNewsDBEntities())
                {
                    User user = db.Users.FirstOrDefault(u => u.first_name.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.Users.Add(new User { first_name = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }