Umbraco:捕获或扩展用户登录



我正在尝试捕获Umbraco用户的登录事件(在CMS中登录)。

我已经尝试从MembersMembershipProvider扩展并覆盖ValidateUser方法。我也改变了网络。配置使用我的类。当我在这个重写的方法中设置断点时,它不会停止并像往常一样登录用户。

public class CustomUmbracoMembershipProvider : Umbraco.Web.Security.Providers.UsersMembershipProvider
{
    public override bool ValidateUser(string username, string password)
    {
        return base.ValidateUser(username, password);
    }
}

正如Jannik所说,Umbraco使用ASP。. NET Identity在最新的几个版本中,因此MembershipProvider不再用于验证和认证用户。

所以经过几个小时的研究,我找到了一个变通的解决方案:

1º-创建一个自定义UserManager,并覆盖方法CheckPasswordAsync:

public class CustomBackOfficeUserManager : BackOfficeUserManager
{
    public CustomBackOfficeUserManager(
        IUserStore<BackOfficeIdentityUser, int> store,
        IdentityFactoryOptions<BackOfficeUserManager> options,
        MembershipProviderBase membershipProvider) :
        base(store, options, membershipProvider)
    {
    }
    /// <summary>
    /// Returns true if the password is valid for the user
    /// </summary>
    /// <param name="user"/><param name="password"/>
    /// <returns/>
    public override Task<bool> CheckPasswordAsync(BackOfficeIdentityUser user, string password)
    {
        //Your implementation
        var result = base.CheckPasswordAsync(user, password).Result;
        return Task.FromResult(result);
    }
}

2º-然后在你自己的启动中,你可以为你的ConfigureUserManagerForUmbracoBackOffice使用这段代码:

var appCtx = ApplicationContext;
        app.ConfigureUserManagerForUmbracoBackOffice<BackOfficeUserManager, BackOfficeIdentityUser>(
            appCtx,
            (options, context) =>
            {
                var membershipProvider = MembershipProviderExtensions.GetUsersMembershipProvider().AsUmbracoMembershipProvider();
                var store = new BackOfficeUserStore(
                    appCtx.Services.UserService,
                    appCtx.Services.ExternalLoginService,
                    membershipProvider);
                return new CustomBackOfficeUserManager(store, options, membershipProvider);
            });

此解决方案将继续使用umbraco UserStore和UserManager的基本方法

我也有类似的情况。这在Umbraco 7.6.5中为我工作。我知道这并不优雅。这只是一个变通方法:

public class MyCustomEvents: ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        base.ApplicationStarted(umbracoApplication, applicationContext);
        UserService.SavedUser += UserServiceSaved;
    }
    private void UserServiceSaved(IUserService sender, SaveEventArgs<IUser> e)
    {
        foreach(IUser user in e.SavedEntities)
        {
            if (!user.IsNewEntity()) //Is not creating a new user
            {
                IUser alreadyLoggedUser = UmbracoContext.Current.Security.CurrentUser;
                if (alreadyLoggedUser == null) //Is not a user management via backoffice
                {
                    if (user.FailedPasswordAttempts == 0) //Is a successful login?
                    {
                        DateTime justNow = DateTime.Now.AddSeconds(-5);
                        if (user.LastLoginDate.CompareTo(justNow) >= 0) //Logged in just now?
                        {
                            //Do your stuff
                        }
                    }
                }
            }
        }
    }
}

由于Umbraco后端使用ASP。. NET Identity在最新的几个版本中,询问您使用的确切版本可能也相关。

我认为ASP。. NET Identity有一个OnLoggedIn事件,您可以尝试:https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.onloggedin.aspx

相关内容

  • 没有找到相关文章

最新更新