C# “如何设置用户状态”;0“;何时自动注销非活动用户?

C# “如何设置用户状态”;0“;何时自动注销非活动用户?,c#,asp.net,authentication,C#,Asp.net,Authentication,我正在aspx页面中使用javascript注销非活动用户。但我还需要在自动注销时更改用户状态“0”。当非活动用户将自动注销时,如何发送值用户状态“0” public void ValidateUser_LOGSTS_Update(object sender, EventArgs e) { int userId = 0; string Cur_user = Session["Userid"].ToString(); String cons

我正在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();
                }


            }

        }

    }

变量t;
window.onload=resetTimer;
document.onkeypress=resetTimer;
函数注销()
{
警报(“您现在已注销。”)
location.href='Login.aspx'
}
函数resetTimer()
{
清除超时(t);
t=setTimeout(注销,60000)//10分钟后注销
}

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

vart;
window.onload=resetTimer;
document.onkeypress=resetTimer;
函数注销()
{
警报(“您现在已注销。”);
location.href='Login.aspx';
$.ajax({
url:“”,
类型:“post”,
数据:`id=${id}`,
成功:功能(响应){
}
})
}
函数resetTimer()
{
清除超时(t);
t=setTimeout(注销,60000)//10分钟后注销
}

@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);
            }

        });

您可以在.cs文件中创建[WebMethod],并使用ajax调用从javascript函数调用该方法。您可以发布您尝试过的解决方案以及收到的错误消息吗?老实说,如果用户刚打开浏览器,登录机制过期,则状态将为挂起。但如果不是这样,您可以按照@KalpeshBhadra的建议创建方法并从视图端调用它。您能给我一个示例代码,用ajax调用从javascript函数调用该方法吗@我是新来的。请给我一些示例代码,以便我能理解@DahicoIn url应该像“control.aspx/ValidateUser_LOGSTS_Update”一样放在id参数“Userid”或username中吗?请让我知道这件事@Shivam TrivediThis不工作了。我无法解决这个问题@Shivam Trivedi
var t;window.onload=重置计时器;document.onkeypress=重置计时器;函数logout(){alert(“您现在已注销。”)location.href='Default.aspx';$.ajax({url:“redirect.aspx/ValidateUser_LOGSTS_Update”,键入:“post”,数据:{msg_sts:0}),成功:函数(响应){console.log(数据+“0”)}函数resetTimer(){clearTimeout(t);t=setTimeout(logout,60000)//10分钟后注销}
似乎您只发送消息状态,我认为您不需要发送此消息。相反,您应该将用户的id发送到服务器,您可以在服务器上获取id,然后使用该id从数据库中获取记录并更新其值。如果仍然不清楚,请为此url提供服务器端代码。我建议你做点别的。下面是错误消息——“错误:找不到根元素”。更新的代码是原始的@Dahico
function logout(){alert(“您现在已注销”).href='redirect.aspx/ValidateUser_LOGSTS_Update';$.ajax({type:“post”,url:“url”,data:{msg_sts:0}),dataType:“json”,success:function(response){console.log('yey');},错误:函数(xhr,ajaxOptions,thrownError){console.log(xhr.status);console.log(thrownError);}}};
$.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);
            }

        });