使用以下详细信息创建 ASP.Net Web 应用程序:

Creating an ASP.Net Web Application with details below:

如何保存用户上次登录的历史记录并在用户登录后立即显示在登录屏幕。

我已经分享了我为用户会话登录实现的代码。我正在检查任何解释方面的帮助,将不胜感激。

用户登录和验证码:

public ActionResult Login(LoginViewModel login)
{
    if (ModelState.IsValid)
    {
        if (new UserEntity().isValidUser(login.EmailId, login.Password))
        {
            Session["login"] = login;
            //Redirect to Employee Controller after Validation
            return RedirectToAction("Index", "Employee");
        }
        else
        {
            ViewBag.InvalidUser = "Invalid User Name or
       Password";
        return View(login);
        }
    }
    return View(login);
}

public Boolean isValidUser(string emailId, string password)
{
    Boolean isValid = false;
    try
    {
        sqlConnection = new SqlConnection(ConnectionString);
        string query = @"Select * from UserProfile
where EmailID='" + emailId + "' and
   Password = '"+password+"'";
    cmd = new SqlCommand(query, sqlConnection);
        sqlConnection.Open();
        SqlDataReader dataReader = cmd.ExecuteReader();
        if (dataReader.Read())
        {
            isValid = true;
        }
    }
    catch (Exception exp)
    {
        //exception logging
    }

    return isValid;
}

这只是尝试并(希望)引导您朝着正确的方向前进。我不确定是否有保存此信息的内置功能,因此我们可能需要手动存储数据。也许它可能是这样的:

public ActionResult Login(LoginViewModel login)
{
    if (ModelState.IsValid)
    {
        SaveLoginTimeToDatabase(); //here, we call a method that we define ourselves
        ... //rest of your original code
    }
    return View(login);
}

public void SaveLoginTimeToDatabase()
{
    DateTime loginTime = new DateTime().Now;
    sqlConnection = new SqlConnection(ConnectionString);
    string query = @"Insert* into UserLoginTimeTable
   VALUES (User.Email, loginTime)";
   //So, we need to create a new database table, UserLoginTimeTable, for this info
   //... Rest of code: open SQL connection, execute etc.
}

但是,我们还需要在用户登录时显示登录时间,所以我们需要一个方法。为此,我们可能会在您的 Razor 登录页面上创建一个字段,用于显示该信息。 然后,在登录方法中,我们调用一个新方法来获取该数据

public ActionResult Login(LoginViewModel login)
{
    if (ModelState.IsValid)
    {
        var lastLogin = FetchLastLoginTime(login.EmailId); //new method for retrieving the data
        // you need to call it before SaveLoginTime...(), so that you dont get the current login, that will be saved in the next line
        SaveLoginTimeToDatabase();
       //you may need to include lastLogin in your loginViewModel:
         login.LastLogin = lastLogin;
        ... //rest of your original code
    }
    return View(login); 
}


public DateTime FetchLastLoginTime(string emailId)
{
    sqlConnection = new SqlConnection(ConnectionString);
    string query = @"Select TOP 1 from UserLoginTimeTable
    Where EmailID = ' emailId"
    //Open sql connection, execute etc
    //Now, you should have access to the last time that user logged in. Return it to the original method and display it
var lastLogin = //get the logintime from the query
return lastLogin;
}

这不是一个明确的答案,可能有不正确的语法,但只是一个整体尝试,以展示您可以如何去做。你不需要接受它作为答案,它只是一个你希望可以工作的建议。由于您需要像这样对数据库进行多次查询,因此您可能需要使用 await/async、work/transactions 或类似的单位来实现一些异步行为,但希望它能正常工作。

总而言之,使用这种方法您需要一个新数据库 table、查询和附加逻辑。随时欢迎您标记 me/write 如果您有更多问题,您认为我可以提供帮助(希望您可以使用其中的一些),祝您好运