ASP.net中的全局OnLoggedIn事件? [英] Global OnLoggedIn event in ASP.net?

查看:169
本文介绍了ASP.net中的全局OnLoggedIn事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户使用ASP.net网站登录时,是否有办法得到通知?

Is there a way to be notified when a user becomes logged in with an ASP.net website?

注意:用户无需访问登录页面"即可登录.如果存在记住我" cookie,则它们可以访问任意页面并登录.

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.

用户登录后,我想获取一些与会话相关的信息.

When a user is logged in i want to fetch some session related information.

注意:存在 Login.LoggedIn 事件.问题是该控件并不存在于每个页面上.并且它位于(Login.aspx)上的一页不会调用OnLoggedIn事件.

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具有全局会话开始时" 通知的方式相同:

In the same way that Global.asax has a global On Session Start notification:

void Session_Start(object sender, EventArgs e) 
{
}

我假设某处有已登录用户通知:

void LoggedIn(object sender, EventArgs e)
{
}

奖金阅读

  • 登录页面ASP.NET上的OnLoggedIn事件
  • 在登录时运行自定义代码
  • MDSN Logon.OnLoggedIn事件
  • 如果设置了记住我",如何更新上次登录日期?
  • Bonus Reading

    • OnLoggedIn event on Login page ASP.NET
    • Run custom code on login
    • MDSN Logon.OnLoggedIn event
    • How to update last login date if "Remember Me" set?
    • 推荐答案

      我认为您没有这样做的独特位置.在我的情况下(MVC + log4net),我使用以下方法:

      I think you don't have a unique place to do that. In my case (MVC + log4net) I use this:

      • Global.asax中,我检查具有预先存在的cookie的经过身份验证的用户.

      • In Global.asax I check for authenticated users with pre-existing 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);
          }
      
      }
      

    • 在我的帐户控制器中,我检查本地登录名(我正在使用MVC4中的Internet应用程序模板,但是如果您使用的是Web表单,则可以在Login.OnLoggedIn中执行此操作)

    • In my Account controller I check for local logins (I'm using Internet Application Template from MVC4, but you can do this in your Login.OnLoggedIn if you're using Web forms)

      [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登录信息,像这样:

    • But I need to check for OAuth logins too, like this:

      [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中的全局OnLoggedIn事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆