I am new to StackOVerflow and this is my first post. Please let me know if I am doing this incorrectly or if there is another way of formatting this to be more conducive to getting help.
I am attempting to track a users activity on a website, but only what webpages they are visiting, their UserID, IP Address, and date and time.
So far, we have gotten the UserID, IPAddress, and DateandTime, to work as well as storing them in a database with an increment logID. However, when we visit a webpage - it's only logging the Activity as the UserActivity page, not any other page that was visited. I have added the code created below and could use some help to determine why it does not log the name of a different webpage on the same website. Thank you.
public static void SaveUserActivity(string FormAccessed)
{
try
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = "mas-application.database.windows.net,1433";
sqlBuilder.UserID = "masadmin";
sqlBuilder.Password = "t3Admin123!!";
sqlBuilder.InitialCatalog = "mas-application";
sqlBuilder.ConnectTimeout = 30;
sqlBuilder.MultipleActiveResultSets = false;
sqlBuilder.PersistSecurityInfo = false;
sqlBuilder.Encrypt = false;
sqlBuilder.TrustServerCertificate = false;
string loggedInUser = HttpContext.Current.User.Identity.Name;
using (SqlConnection sqlConnection = new SqlConnection(sqlBuilder.ConnectionString))
{
sqlConnection.Open();
StringBuilder sb = new StringBuilder();
sb.Append("Insert into UserActivity(UserIP, DateOfActivity, UserID, FormAccessed) values(" +
"'" + GetIP4Address() + "'," +
"'" + DateTime.Now + "'," +
"'" + loggedInUser + "'," +
"'" + FormAccessed + "')");
String sql = sb.ToString();
SqlCommand command = new SqlCommand(sql, sqlConnection);
command.ExecuteNonQuery();
sqlConnection.Close();
}
}
catch (SqlException ex)
{
}
}
// This function gets the IP Address
public static string GetIP4Address()
{
string IP4Address = string.Empty;
foreach (IPAddress IPA in
Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
if (IP4Address != string.Empty)
{
return IP4Address;
}
foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
return IP4Address;
}
}
}
Aucun commentaire:
Enregistrer un commentaire