gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:48:19 30 4
gpt4 key购买 nike

当用户登录 ASP.net 网站时,有没有办法收到通知?

Note: A user can become logged in without visiting a "login page". If the "remember me" cookie exists, they can hit an arbitrary page and be logged in.



当用户登录时,我想获取一些与 session 相关的信息。

Note: There is the Login.LoggedIn event. Problem is that that control doesn't exist on every page; and the one page it is present on (Login.aspx) doesn't call OnLoggedIn event.



以同样的方式 Global.asax有一个全局的 On Session Start 通知:
void Session_Start(object sender, EventArgs e) 
{
}

我假设某处有一个 On User Logged In 通知:
void LoggedIn(object sender, EventArgs e)
{
}

奖励阅读
  • OnLoggedIn event on Login page ASP.NET
  • Run custom code on login
  • MDSN Logon.OnLoggedIn event
  • 如果设置了“记住我”,如何更新上次登录日期?
  • 最佳答案

    我认为你没有一个独特的地方来做到这一点。在我的情况下(MVC + log4net)我使用这个:

  • Global.asax我使用预先存在的 cookie 检查经过身份验证的用户。
    protected void Session_Start()
    {
    string ip = HttpContext.Current.Request.UserHostAddress;

    log.InfoFormat("Starting session: {0} from {1}.",Session.SessionID, ip);

    if ((HttpContext.Current != null) &&
    (HttpContext.Current.User != null) &&
    (HttpContext.Current.User.Identity.IsAuthenticated) )
    {
    string user = HttpContext.Current.User.Identity.Name;
    string type = "Cookie";

    log.InfoFormat("User {0} logged in with {1}.", user, type);
    }

    }
  • 在我的帐户 Controller 中,我检查本地登录(我使用的是来自 MVC4 的 Internet 应用程序模板,但如果您使用的是 Web 表单,则可以在 Login.OnLoggedIn 中执行此操作)
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
    if (ModelState.IsValid && WebSecurity.Login(model.EMail, model.Password, persistCookie: model.RememberMe))
    {
    string user = model.EMail;
    string type = "Forms";

    log.InfoFormat("User {0} logged in with {1}.", user, type);

    return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    log.ErrorFormat("Bad password or user name. User={0}", model.EMail, model.Password);
    return View(model);
    }
  • 但我也需要检查 OAuth 登录,如下所示:
    [AllowAnonymous]
    public ActionResult ExternalLoginCallback(string returnUrl)
    {
    AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
    if (!result.IsSuccessful)
    {
    log.Debug("External login failure.");

    return RedirectToAction("ExternalLoginFailure");
    }

    if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
    {
    log.InfoFormat("User {0} logged in with External {1} login. External UserID = {2}",
    Membership.GetUser(OAuthWebSecurity.GetUserName(result.Provider, result.ProviderUserId)).UserName,
    result.Provider,
    result.ProviderUserId);

    return RedirectToLocal(returnUrl);
    }

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

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