Copied to clipboard

Flag this post as spam?

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


  • Janis Z 3 posts 75 karma points
    Jul 18, 2018 @ 07:46
    Janis Z
    1

    CheckPasswordAsync - create and sign in user. Custom authentication

    Hi,

    I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API and, if credentials are correct, create user (if this is the first time user authenticates) and sign in user.

    I'm using Umbraco's IdentityExtensions and I'm implementing IBackOfficeUserPasswordChecker interface. I've managed to done the creating user part, however, I don't know how to sign in user after creating it.

    My code:

        public Task<BackOfficeUserPasswordCheckerResult> CheckPasswordAsync(BackOfficeIdentityUser user, string password)
        {
            Log.Info("Auth logging");
            AuthResponseModel authData = null;
    
            try
            {
                authData = PerformExternalAuth(user.UserName, password);
            }
            catch(Exception ex)
            {
                Log.Error(ex.ToString());
                return Task.FromResult(BackOfficeUserPasswordCheckerResult.FallbackToDefaultChecker);
            }
    
            var userService = ApplicationContext.Current.Services.UserService;
            var userAlreadyExists = userService.GetByEmail(user.UserName) != null || userService.GetByUsername(user.UserName) != null;
            if (userAlreadyExists == false)
            {
                Log.Info("Creating new memeber");
                var userCreated = CreateUser(authData.UserName, password, authData.Email, userService);
    
                if(userCreated == false)
                {
                    Log.Info("Error while trying to create user");
                    return Task.FromResult(BackOfficeUserPasswordCheckerResult.InvalidCredentials);
                }
            }
    
            return Task.FromResult(BackOfficeUserPasswordCheckerResult.ValidCredentials);
        }
    
        private bool CreateUser(string username, string password, string email, IUserService userService)
        {
            // Create user identity
            var newUser = userService.CreateWithIdentity(username, email, password, userService.GetDefaultMemberType());
    
            // Set hashed password
            newUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage(password);
    
            // Manage user groups
            var adminGroup = userService.GetUserGroupByAlias("admin") as IReadOnlyUserGroup;
            var sensitiveDataGroup = userService.GetUserGroupByAlias("sensitiveData") as IReadOnlyUserGroup;
    
            newUser.AddGroup(adminGroup);
            newUser.AddGroup(sensitiveDataGroup);
    
            userService.Save(newUser);
    
            return true;
        }
    

    As far as I can tell BackOfficeSignInManager could be user, but how do I initialize it? What are other ways to authenticate user after creating it?

    Side note: user authentication (sign in) works for users who already exist in database.

    Thanks!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 12, 2018 @ 15:01
    Alex Skrypnyk
    0

    Hi Janis

    I'm implementing custom authentication in Umbraco. My aim is to validate user credentials (username + password) against external API, a similar task as you had.

    Did you find a way of doing it? Can you share with us?

    The problem I'm struggling now is how to manage user groups externally? Is it possible to somehow? And what is possible in user groups functionality to be overridden?

    Thanks,

    Alex

  • Janis Z 3 posts 75 karma points
    Nov 12, 2018 @ 22:05
    Janis Z
    1

    Hi! I didn't solve the problem. You can create groups programmatically and use the code seen in my snippet to assign them to users.

Please Sign in or register to post replies

Write your reply to:

Draft