【发布时间】: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