【问题标题】:How to set user status "0" when automatically logout inactive user?自动注销非活动用户时如何设置用户状态“0”?
【发布时间】:2020-02-13 05:25:34
【问题描述】:

我在 aspx 页面中使用 javascript 来注销非活动用户。但我也需要在自动注销时更改用户状态“0”。当非活动用户自动注销时,如何发送值用户状态“0”?

public void ValidateUser_LOGSTS_Update(object sender, EventArgs e)
    {
        int userId = 0;

        string Cur_user = Session["Userid"].ToString();
        String constr = ConfigurationManager.ConnectionStrings["abcd"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("sp_tbl_LogCheck_update"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@userID", Cur_user);
                cmd.Parameters.Add("@msg", SqlDbType.Int, 20).Direction = ParameterDirection.Output;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();

                // read output value from @NewId
                string msg_sts = Convert.ToString(cmd.Parameters["@msg"].Value);

                con.Close();
                if (msg_sts == "0")
                {
                    Session.Clear();
                    Session.Abandon();
                    //Response.Redirect(FormsAuthentication.LoginUrl);
                    FormsAuthentication.RedirectToLoginPage();
                }


            }

        }

    }
<script type="text/javascript">

    var t;
    window.onload=resetTimer;
    document.onkeypress=resetTimer;

    function logout()
    {
        alert("You are now logged out.")
        location.href = 'Login.aspx'
    }
    function resetTimer()
    {
        clearTimeout(t);
        t=setTimeout(logout,60000) //logs out in 10 min
    }

    </script>

【问题讨论】:

  • 您可以在 .cs 文件中创建 [WebMethod] 并使用 ajax 调用从您的 javascript 函数中调用该方法。
  • 您能发布您尝试过的解决方案以及收到的错误消息吗?
  • 老实说,如果用户只是关闭浏览器并且登录机制到期,则状态将被挂起。但如果不是这种情况,您可以创建方法并按照@KalpeshBhadra 的建议从您的视图中调用它。
  • 您能否给我示例代码以使用 ajax 调用从 javascript 函数调用该方法? @KalpeshBhadra
  • 我是新手。请给我一些示例代码,以便我可以遵循。 @Dahico

标签: c# asp.net authentication


【解决方案1】:

您可以在注销用户时向您的服务器发送 ajax 请求。为此,您必须向您要注销的当前用户发送 id 参数,在成功响应时,您可以执行任何任务。

var t;
window.onload=resetTimer;
document.onkeypress=resetTimer;

function logout()
{
    alert("You are now logged out.");
    location.href = 'Login.aspx';
$.ajax({
url:"",
type:"post",
data:`id=${id}`,
success:function(response){
}
})
}
function resetTimer()
{
    clearTimeout(t);
    t=setTimeout(logout,60000) //logs out in 10 min
}

【讨论】:

  • 我应该在 url 中输入“control.aspx/ValidateUser_LOGSTS_Update”和 id 参数“Userid”还是用户名?请让我知道这件事。 @Shivam Trivedi
  • 这不起作用。我无法弄清楚这个问题......@Shivam Trivedi
  • &lt;script type="text/javascript"&gt;var t;window.onload = resetTimer;document.onkeypress = resetTimer;function logout() {alert("You are now logged out.")location.href = 'Default.aspx';$.ajax({ url: "redirect.aspx/ValidateUser_LOGSTS_Update", type: "post", data: "{msg_sts: 0 }", success: function (response) { console.log(data + "0"); } }) } function resetTimer() { clearTimeout(t); t = setTimeout(logout, 60000) //logs out in 10 min } &lt;/script&gt;
  • 好像你只发送 msg status ,我认为你不需要发送这个。相反,您应该将用户的 id 发送到可以获取 id 的服务器,然后使用该 id 从数据库中获取记录并更新其值。
  • 如果还有不清楚的地方,请提供此网址的服务器端代码。我会建议你别的。
【解决方案2】:

@Faisal 请在原始问题中添加代码以便于阅读。请使用此语法发布。还要确保正确使用 url。还要检查您的控制台是否有错误消息并将其发布在此处以便在出现错误时取得进展。

$.ajax({
            type: "post",
            url: "url",
            data: {},
            dataType: "json",
            success: function (response) {
                console.log('yey');
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
            }

        });

【讨论】:

  • 这是错误消息 - “错误:未找到根元素”。更新的代码在原始问题中。 @Dahico
  • function logout() {alert("You are now logged out.") location.href = 'redirect.aspx/ValidateUser_LOGSTS_Update'; $.ajax({ type: "post", url: "url", data: "{ msg_sts: 0 }", dataType: "json", success: function (response) { console.log('yey'); },error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); console.log(thrownError); } }); &lt;/script&gt;
猜你喜欢
  • 1970-01-01
  • 2014-09-28
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2020-08-22
  • 2023-03-16
相关资源
最近更新 更多