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
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);
}
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.
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.
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;
}
}
This code works well on Umbraco 8. I am trying to replicate the same scenario in Umbraco 9 (execute custom code on login and signout) but unfortunately OWIN methods do not exist in .NET Core/5 thus Umbraco 9. What methods are available or is there any other way I could do this for Umbraco 9. Your reply would be mostly appreciated.
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
Hi Harry
Do you want an user authenticating event?
You can define is user logged in with this code:
Thanks
Alex
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
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
I'll give this ago thanks!
Thanks, Harry
You are welcome, Harry.
Have a nice weekend!
/Alex
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/
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.
This code works well on Umbraco 8. I am trying to replicate the same scenario in Umbraco 9 (execute custom code on login and signout) but unfortunately OWIN methods do not exist in .NET Core/5 thus Umbraco 9. What methods are available or is there any other way I could do this for Umbraco 9. Your reply would be mostly appreciated.
Jason, do you manage to figure this out?
is working on a reply...