Search code examples
c#orchardcmsorchardcms-1.8

Orchard 1.8 - How to trigger action when user logs in


I created a custom part that I attached to the User part (I need it to store additional data related to the user). I need to execute some code when the user registers the account or each time he authenticates himself using that account. I put the code to execute during the registration in the handler of my custom part, inside the OnCreated method, but I cannot find how to identify the login operation. Is there a method of the handler where I can put that code that I need to execute on each login?


Solution

  • That code won't be in the part but in the IUserEventHandler LoggedIn event.

    So an example implementation would be:

    public class ModUserEvents : IUserEventHandler {
        public ModUserEvents() {
        }
    
        public void LoggedIn(IUser user) {
            // go go do your stuff
        }
    
    
        #region unused events
        public void Approved(IUser user){
        }
    
        public void Created(UserContext context){
        }
    
        public void Creating(UserContext context) {
        }
    
        public void LoggedOut(IUser user) {
        }
    
        public void AccessDenied(IUser user) {
        }
    
        public void ChangedPassword(IUser user) {
        }
    
        public void SentChallengeEmail(IUser user) {
        }
    
        public void ConfirmedEmail(IUser user) {
        }
        #endregion
    }
    

    You'll need to reference Orchard.Users and add @using Orchard.Users.Events