gpt4 book ai didi

asp.net - ASP.net 中的全局 OnLoggedOut 事件?

转载 作者:行者123 更新时间:2023-12-02 12:17:25 24 4
gpt4 key购买 nike

当用户从 ASP.net 网站注销时是否可以收到通知?

Note: A user can become logged out without visiting or clicking a "logout" link.

当用户注销时,我想获取清除一些 session 相关信息,并写入数据库。

Note: There is the LoginStatus.OnLoggedOut event. Problem is that that control doesn't exist on every page, nor does the user have to use a LoginStatus control in order to logout (e.g.when the login times out, or the user is logged out)

与 Global.asax 具有全局 session 停止通知的方式相同:

void Session_End(object sender, EventArgs e) 
{
}

我假设某处有“用户注销”通知:

void LoggedOut(object sender, EventArgs e)
{
}

奖励阅读

最佳答案

我不同意。有时您想知道用户何时注销。例如,我有一个应用程序需要扫描所有登录的用户,以查看管理层是否有“任何人在家”。有时,经理可能正在浏览网站,但并未实际登录。他们的 session 可能仍处于事件状态,因为经理最近已注销,但仍在网站上浏览,而软件需要知道这一点。

在本例中,我制作了 HttpRuntime.Cache 逻辑来捕获登录事件和数据,包括用户名和 IP 地址、用户名等。此方法在以下位置调用:

public static void Application_AuthenticateRequest(对象发送者,EventArgs e)

然后,我使用该数据执行逻辑,计算登录状态和其他有用的应用程序详细信息。

以下内容很快且未经测试,但如果您问此类问题,我相信您可以清理它:)。我希望它有帮助。

 public static void HandleUserLoginLogic()
{
string UserName = HttpContext.Current.User.Identity.Name.ToString();
if (UserName != null)
{
// if the user has logged in but we have not performed logic, do it now
if (HttpRuntime.Cache["Authenticated_" + UserName] == null)
{
// absolutely confirm the user has logged in
if (UsrMan.IsUserLoggedIn())
{
// SETUP MY RUNTIME VARIABLES NOW
HttpRuntime.Cache["AuthenticatedIPAddress_" + System.Web.HttpContext.Current.Request.UserHostAddress] = UserName;
HttpRuntime.Cache["Authenticated_" + UserName] = true;
String[] roles = UsrMan.GetRolesForUser(UserName);
// handle roles for this user for future application uses in the future.
foreach (string role in roles)
{
// handle first time condition
if (HttpRuntime.Cache["AuthenticatedUserInRole_" + role] != null)
{
StringCollection scUserRole = (StringCollection)HttpRuntime.Cache["AuthenticatedUserInRole_" + role];
if (!scUserRole.Contains(UserName))
{
scUserRole.Add(UserName);
HttpRuntime.Cache["AuthenticatedUserInRole_" + role] = scUserRole;
}
}
// handle standard condition
else
{
StringCollection scUserRole = new StringCollection();
scUserRole.Add(UserName);
HttpRuntime.Cache["AuthenticatedUserInRole_" + role] = scUserRole;
}
}
}
}
}

// HANDLE LOGGED OUT CONDITION
if ((HttpRuntime.Cache["AuthenticatedIPAddress_" + System.Web.HttpContext.Current.Request.UserHostAddress] != null) && (UserName == null))
{
string OldUserName = HttpRuntime.Cache["AuthenticatedIPAddress_" + System.Web.HttpContext.Current.Request.UserHostAddress].ToString();
HttpRuntime.Cache["Authenticated_" + OldUserName] = null;
String[] roles = UsrMan.GetRolesForUser(UserName);
foreach (string role in roles)
{
StringCollection scUserRole = (StringCollection)HttpRuntime.Cache["AuthenticatedUserInRole_" + role];
scUserRole.Remove(UserName);
if (scUserRole.Count > 0)
HttpRuntime.Cache["AuthenticatedUserInRole_" + role] = scUserRole;
else
HttpRuntime.Cache["AuthenticatedUserInRole_" + role] = null;
}
}
}
public static bool IsUserLoggedIn()
{
bool result = false;
if (HttpContext.Current.User != null &&
HttpContext.Current.User.Identity != null
&& HttpContext.Current.User.Identity.IsAuthenticated)
{ result = true; }
return result;
}

关于asp.net - ASP.net 中的全局 OnLoggedOut 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11003660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com