Ejemplo n.º 1
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());
            }
        }