OnLoggedIn global event in ASP.net?

Is there a way to get notified when a user is registered with ASP.net?

Note . The user can log in without visiting the "login page". If the Remember Me cookie exists, it can go to an arbitrary page and log in.

When a user logs in, I want to get some information related to the session.

Note . There is an event Login.LoggedIn. The problem is that this control does not exist on every page; and one page on which it is present ( Login.aspx) does not raise an event OnLoggedIn.

Just like it Global.asaxhas a global session start notification in a session:

void Session_Start(object sender, EventArgs e) 
{
}

I assume somewhere there is an On User Logged In notification:

void LoggedIn(object sender, EventArgs e)
{
}

Reading bonuses

+5
source share
4 answers

I think that you do not have a unique place for this. In my case (MVC + log4net) I use this:

  • In Global.asaxI check for authenticated users with an 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);
        }
    
    }
    
  • In my account controller, I check local logins (I use the Internet application template from MVC4, but you can do it in your own Login.OnLoggedInif you use 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);
    }
    
  • But I need to also check for OAuth logins, for example:

    [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);
        }
    
        ...
    }
    
+7

Application_AuthenticateRequest global.asax, , , , , , .

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    //  check for logged in or not
    if (null != authCookie)
    {
        // is logged in... check if the session needs init

    }   
}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    //  check for logged in or not
    if(HttpContext.Current.User != null && 
        HttpContext.Current.User.Identity != null 
            && HttpContext.Current.User.Identity.IsAuthenticated)
    {
        // is logged in... check if the session needs init

    }   
}    
+3

: OnLoggedIn Login, ( Session_Start Global.asax), . , , , , .

+2

, , , .

, - :

  • /

: " , ".

, , :

  • , .
  • , , , .

, , / .

:

  • , " " ​​ Login.OnLoggedIn, Session_Start Global.asax. , , , , .
  • , " ", , Session_End Global.asax. , , Session_End , . , .
  • " " ​​ . , IE " " "". IE, cookie IE-. , , , .

" " . som, , . . , - , .

+2
source

All Articles