Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Harry Stewart 12 posts 92 karma points
    Apr 07, 2017 @ 10:40
    Harry Stewart
    0

    Detect backoffice user login and logout

    I'm creating a package which requires me to detect when a user in the back office logins or logs out does anyone know the best practice for this or if there is an event I can hook into

  • Alex Skrypnyk 4512 posts 16136 karma points MVP admin
    Apr 07, 2017 @ 11:02
    Alex Skrypnyk
    0

    Hi Harry

    Do you want an user authenticating event?

    You can define is user logged in with this code:

    using Umbraco.Core.Security;
    var ticket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    if (ticket != null) {
    var user = Umbraco.Core.ApplicationContext.Current.Services.UserService.GetByUsername(ticket.Name);
    }
    

    Thanks

    Alex

  • Harry Stewart 12 posts 92 karma points
    Apr 07, 2017 @ 11:10
    Harry Stewart
    0

    Ah okay thanks, I've been using that so far but I need to receive or be able to hook into an event that I can override when the User logins and logs out, as what you suggested only gets the current context when you request the context and I don't wanna have to poll th server.

    Thanks, Harry

  • Alex Skrypnyk 4512 posts 16136 karma points MVP admin
    Apr 07, 2017 @ 11:24
    Alex Skrypnyk
    1

    Haryy

    Did you try to hook into membership events?

    I think it's possible to do like in this post:

    https://our.umbraco.org/forum/developers/api-questions/30522-How-to-subscribe-to-backend-login-event-if-existing-else-alternative-solution#comment-113339

    Thanks,

    Alex

  • Harry Stewart 12 posts 92 karma points
    Apr 07, 2017 @ 11:29
    Harry Stewart
    0

    I'll give this ago thanks!

    Thanks, Harry

  • Alex Skrypnyk 4512 posts 16136 karma points MVP admin
    Apr 07, 2017 @ 11:29
    Alex Skrypnyk
    0

    You are welcome, Harry.

    Have a nice weekend!

    /Alex

  • Dan Diplo 1269 posts 4652 karma points MVP c-trib
    Apr 07, 2017 @ 11:32
    Dan Diplo
    1

    It doesn't look, to me, like there are any events raised in the BackOfficeSignInManager class Umbraco (I think) uses for this. Maybe something in the base classes, but I'm not aware that Owin authentication has any events.

    All I can think is that you'd have to implement a custom Identity sign-in manager that raises an event and then calls the Umbraco one. I'm not sure if this is possible, but generally Identity is extensible and pluggable. There's some info on this in this Issue here: http://issues.umbraco.org/issue/U4-7032 and https://shazwazza.com/post/configuring-aspnet-identity-oauth-login-providers-for-multi-tenancy/

  • Bernard Moes 1 post 71 karma points
    6 days ago
    Bernard Moes
    0

    With the newer Umbraco versions you would have to override authentication methods in OWIN to execute custom code on login and signout. For my project I had to do something similar. The code sample below will probably help you. I don't have a sample for the logout part but this will probably guide you in the right direction.

    public class Startup : UmbracoDefaultOwinStartup
    {
        protected override void ConfigureUmbracoUserManager(IAppBuilder app)
        {
            app.ConfigureUserManagerForUmbracoBackOffice<BackOfficeUserManager, BackOfficeIdentityUser>(
                ApplicationContext,
                (options, context) =>
                {
                    var membershipProvider = MembershipProviderExtensions.GetUsersMembershipProvider().AsUmbracoMembershipProvider();
                    var settingContent = UmbracoConfig.For.UmbracoSettings().Content;
    
                    var userManager = BackOfficeUserManager.Create(options,
                        ApplicationContext.Current.Services.UserService,
                        ApplicationContext.Current.Services.EntityService,
                        ApplicationContext.Current.Services.ExternalLoginService,
                        membershipProvider,
                        settingContent);
    
                    userManager.BackOfficeUserPasswordChecker = new CustomBackofficePasswordChecker();
    
    
                    return userManager;
                });
        }
    }
    
    public class CustomBackofficePasswordChecker : IBackOfficeUserPasswordChecker
    {
        public async Task<BackOfficeUserPasswordCheckerResult> CheckPasswordAsync(BackOfficeIdentityUser user, string password)
        {
            //Execute custom code
    
            return BackOfficeUserPasswordCheckerResult.FallbackToDefaultChecker;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft