Esempio n. 1
0
        public ActionResult DeleteConfirmed(int id)

        {
            try
            {
                ModelCollections.userAccounts.RemoveAt(id);

                //log user activity
                //any logger can be used as well, this a custom example
                //adds data to a collection, this can be written to db directly or to a log file
                ActivityString activity = LogActivity.DeleteUser();
                int.TryParse(Session["UserID"].ToString(), out int ID);
                int recordID = ModelCollections.userActivityLogs.Count + 1;
                ModelCollections.userActivityLogs.Add(new UserActivityLog()
                {
                    ID                  = recordID,
                    UserAccountID       = ID,
                    Username            = Session["Username"].ToString(),
                    Activity            = activity.Activity,
                    ActivityDate        = DateTime.Now,
                    ActivityDescription = activity.ActivityDescription,
                    Error               = "", //if there was an error, you can report it here as well
                    Source              = System.Environment.MachineName
                });

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
Esempio n. 2
0
        public ActionResult LogOff()
        {
            try
            {
                ActivityString activity = LogActivity.LogoutUser();
                int.TryParse(Session["UserID"].ToString(), out int id);

                int recordID = ModelCollections.userActivityLogs.Count + 1;
                ModelCollections.userActivityLogs.Add(new UserActivityLog()
                {
                    ID                  = recordID,
                    UserAccountID       = id,
                    Username            = Session["Username"].ToString(),
                    Activity            = activity.Activity,
                    ActivityDate        = DateTime.Now,
                    ActivityDescription = activity.ActivityDescription,
                    Error               = "", //if there was an error, you can report it here as well
                    Source              = System.Environment.MachineName
                });

                Session["UserRole"] = null;
                Session["Username"] = null;
                Session["UserID"]   = null;
            }
            catch (Exception ex)
            {
                // Info
                throw ex;
            }
            // Info.
            return(this.RedirectToAction("Login", "Login"));
        }
Esempio n. 3
0
        public ActionResult Create([Bind(Include = "ID,Username,UserPassword,ConfirmPassword,Name,LastName,Role,LastLoginDate")] UserAccount userAccount)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //it is advisable to encrypt password before storing it
                    //in this example no encryption will be used

                    //if db is used, insert record to db here
                    userAccount.ID = ModelCollections.userAccounts.Count + 1;
                    ModelCollections.userAccounts.Add(userAccount);

                    //log user activity
                    //any logger can be used as well, this a custom example
                    //adds data to a collection, this can be written to db directly or to a log file
                    ActivityString activity = LogActivity.CreateUser();
                    int.TryParse(Session["UserID"].ToString(), out int id);
                    int recordID = ModelCollections.userActivityLogs.Count + 1;
                    ModelCollections.userActivityLogs.Add(new UserActivityLog()
                    {
                        ID                  = recordID,
                        UserAccountID       = id,
                        Username            = Session["Username"].ToString(),
                        Activity            = activity.Activity,
                        ActivityDate        = DateTime.Now,
                        ActivityDescription = activity.ActivityDescription,
                        Error               = "", //if there was an error, you can report it here as well
                        Source              = System.Environment.MachineName
                    });

                    return(RedirectToAction("Index"));
                }
                return(View(userAccount));
            }
            catch (Exception ex)
            {
                return(View());
            }
        }
Esempio n. 4
0
        public ActionResult Login(LoginViewModel user, string returnUrl)
        {
            ViewResult viewResult = View(user);

            try
            {
                UserAccount userAccount = null;
                // this action is for handle post (login)
                if (ModelState.IsValid) // this is check validity
                {
                    if (user != null)
                    {
                        //validate user
                        userAccount = ModelCollections.userAccounts.Find(x => x.Username == user.Username);
                        // var index = ModelCollections.userAccounts.FindIndex(x => x.Username == user.Username);
                        //here index is ID in userAccount so the line above is not necessary

                        if (userAccount != null)
                        {
                            if (userAccount.Username != null)
                            {
                                if (userAccount.UserPassword != null)
                                {
                                    //decrypt found password if necessary and compare it to the one provided in login
                                    if (user.Password == userAccount.UserPassword)
                                    {
                                        Session["UserRole"] = userAccount.Role;
                                        Session["Username"] = userAccount.Username;
                                        Session["UserID"]   = userAccount.ID;
                                        Session["FullName"] = userAccount.Name + " " + userAccount.LastName;

                                        ActivityString activity = LogActivity.LoginUser();
                                        int.TryParse(Session["UserID"].ToString(), out int id);

                                        int recordID = ModelCollections.userActivityLogs.Count + 1;
                                        ModelCollections.userActivityLogs.Add(new UserActivityLog()
                                        {
                                            ID                  = recordID,
                                            UserAccountID       = id,
                                            Username            = Session["Username"].ToString(),
                                            Activity            = activity.Activity,
                                            ActivityDate        = DateTime.Now,
                                            ActivityDescription = activity.ActivityDescription,
                                            Error               = "", //if there was an error, you can report it here as well
                                            Source              = System.Environment.MachineName
                                        });

                                        //Add login date to user who logged in
                                        ModelCollections.userAccounts[userAccount.ID].LastLoginDate = DateTime.Now;

                                        return(RedirectToLocal(returnUrl));
                                    }
                                    else
                                    {
                                        ModelState.AddModelError(String.Empty, "Incorrect password");
                                    }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError(String.Empty, "Username does not exist");
                            }
                        }
                        else
                        {
                            ModelState.AddModelError(String.Empty, "User account does not exist");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error. Check log for details."); //there should be a log
            }
            return(viewResult);
        }