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

时间:2019-10-16 12:17:57

标签: 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 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>

2 个答案:

答案 0 :(得分:0)

那么您可以在注销用户的同时将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
}

答案 1 :(得分:0)

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

        });