Example #1
0
        public ActionResult Create([Bind(Exclude = "LastLogin")] UserViewModel userVm)
        {
            if (string.IsNullOrWhiteSpace(userVm.Password))
                ModelState.AddModelError("Password", "Password not be empty");

            if (ModelState.IsValid)
            {
                try
                {
                    using (var db = EpidaurusDbContainer.Create())
                    {
                        var user = new User()
                        {
                            Username = userVm.Username,
                            Name = userVm.Name,
                            Password = SecurityService.GetPasswordHash(userVm.Password),
                            Roles = userVm.Roles ?? "",
                            LastLogin = new DateTime(2000, 1, 1),
                        };

                        db.AddToUsers(user);
                        db.SaveChanges();
                    }

                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    ModelState.AddModelError("", "DB Error! " + ex.Message);
                }
            }

            return View(userVm);
        }
Example #2
0
        private static void LoginUser(EpidaurusDbContainer db, User user)
        {
            user.LastLogin = DateTime.Now;
            db.SaveChanges();

            var roles = user.Roles.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
            var principal = new EpiPrincipal(user.Username, roles);
            SetCurrentPrincipal(principal);
            _log.Trace("Loginuser: User: {0} Roles: {1}", user.Username, user.Roles);
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="username">Initial value of the Username property.</param>
 /// <param name="password">Initial value of the Password property.</param>
 /// <param name="lastLogin">Initial value of the LastLogin property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="roles">Initial value of the Roles property.</param>
 public static User CreateUser(global::System.String username, global::System.String password, global::System.DateTime lastLogin, global::System.String name, global::System.String roles)
 {
     User user = new User();
     user.Username = username;
     user.Password = password;
     user.LastLogin = lastLogin;
     user.Name = name;
     user.Roles = roles;
     return user;
 }