Copied to clipboard

Flag this post as spam?

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


  • Paolo Siviero 1 post 71 karma points
    Nov 18, 2019 @ 10:19
    Paolo Siviero
    0

    First login backoffice not working - override backoffice password checker

    Hi everyone!

    I've got my project in Umbraco which overrides the method CheckPasswordAsync in a Custom class which uses the 'IBackOfficeUserPasswordChecker' interface to be able to sign in LDAP users from another domain.

    Inside the method, I'm calling an authentication API which gives me a list of roles that the user is enabled to. Consequently I'm running a foreach statement where I'm creating/updating Members/Users based on the specific roles (e.g. if I receive the role 'Base' I'll create just a member, if I'll receive the roles 'Admin' I'll create both a member and a user) and so on.

    It works perfectly fine in almost all scenarios (e.g. front office works like a charm) but in the back office I get a 'Login failed for user ...' for the first login attempt, then at the second it let me enter the Umbraco backoffice. It's like it won't recognize the newly created user at first with an identity in ASP.NET Identity.

    Here's the user creation method:

        private static int createUser(string username, string password, List<string> roleNames = null)
        {
            // Obtain current user service
            var userService = Current.Services.UserService;
            // Try to obtain a user with the given email in case one already exists
            var user = userService.GetByUsername(username);
            if (user != null)
                return user.Id;
    
            // Create a new user
            var newUser = userService.CreateUserWithIdentity(username, username);
            // Get user group as IReadOnlyUserGroup
            var userGroup = userService.GetUserGroupByAlias("admin") as IReadOnlyUserGroup;
            // Add the userGroup to the newUser
            newUser.AddGroup(userGroup);
            // Set the user's password
            newUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage(password); ;
            // Save the new user
            userService.Save(newUser);
    
            return newUser.Id;
        }
    enter code here
    

    And here's the PasswordCheckerAsync override:

            public async Task<BackOfficeUserPasswordCheckerResult> CheckPasswordAsync(BackOfficeIdentityUser user, string password)
        {
            MemberLoginModel model = new MemberLoginModel() { Username = user.UserName, Password= password };
            var autentica = await MemberUserService.Autenticate(model, Services.LoginType.Backend);
            VenetaHelpers.LogInfo(typeof(MemberUserService), Newtonsoft.Json.JsonConvert.SerializeObject(autentica.Content), model.Username);
    
            //Login backend
            var result = (autentica.Status == System.Net.HttpStatusCode.OK)
                ? BackOfficeUserPasswordCheckerResult.ValidCredentials
                : BackOfficeUserPasswordCheckerResult.InvalidCredentials;
    
            return result;
        }
    

    Inside the MemberUserService,Autenticate there's the custom logic used to identify whether to create a user, a member or both. (Or updating the roles if they already exist)

Please Sign in or register to post replies

Write your reply to:

Draft