Copied to clipboard

Flag this post as spam?

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


  • Jan Arenö 9 posts 40 karma points
    Apr 16, 2015 @ 10:18
    Jan Arenö
    1

    Create Membership provider with mix of umbraco membership provider and external

    Hi. Using umbraco 7.1.x

    I'm trying to do a mixture of using the standard umbraco membership and adding "support" by an external db

    Scenario:
    I have a big db with about 300k users. Some of these will log in to my umbraco site. When they do I will try to log them in as usual, if that do not succed then I look them up in the external db and create a user with a property with an external id.

    In umbraco they have a username and a password. In the external db they have a id and another field as password, let's call it external_password

    When a user login I would like to do it like so:

    * check if user can login to umbraco db using username/password
    * if not, check if I can find the user using the external id. If so, test if username = external id and with password.
    * if not, check external db if user used external id and external_password. If so, create a umbraco user and sign him in.

    The reason to store the user in both places is because in umbraco, we will add media and more data to the member and also have the access rights handled by umbraco.

    I think I could do this by implementing a custom membership provider, extending UmbracoMembershipProvider and overriding validateUser

    Now to my question.

    I think I know how to add a custom property to umbraco memebers (external_id) but I can't figure out the best way to search for it. The membership provider does only give access to username/email in find functions, or a getAllUsers. Is the only way GetAllUsers and then iterate? that dosn't seems like good performance.

    Does my idea how to solve this work, or can someone point out a better solution?

     

  • ggesheva 14 posts 114 karma points
    May 27, 2020 @ 11:03
    ggesheva
    0

    I am looking for similar solution. Did you manage to achieve it somehow ?

  • Jan A 43 posts 248 karma points
    May 27, 2020 @ 11:25
    Jan A
    1

    I belive so. I guess it's the search for external id, or is it all of it?

    This function does that search

        public static IEnumerable<IMember> GetMembersByExternalId(int externalId, bool includeLockedOutMembers = false)
        {
            var members = ApplicationContext.Current.Services.MemberService.GetMembersByPropertyValue("externalid", externalId);
    
            if (!includeLockedOutMembers)
                members = members.Where(m => !m.GetValue<bool>("umbracoMemberLockedOut"));
    
            return members;
        }
    

    The login tries to login to the membership provider. If it fails, try to get the user by externalId, and login to membership provider. If that also failes, it will also try to fetch the user from the external db. If successful it stores the external user into umbraco, and adds the externalId to the member (custom field) and login that user. I think it's something like this... (may be some error in the code below, but it may bring you on track)

    var user = GetMemberByExternalId(externalId);
    if (user != null && Membership.ValidateUser(user.Username, password))
                {
                    umbracoUsername = user.Username;
                    return true;
                }
    
    var externalUser = userService.GetUser(externalId);
    if(externalIUser == null) {
        return false;
    }
    
    if(user == null && externalUser.password == password)   // we don't want to create if user is found but with wrong password and also if password don't match external db
    {
        user = CreateMember(externalUser);   // function that creates the member in umbraco
        if(Membership.ValidateUser(user.Username, password))
        {
            return true;
        }
    }
    
    return false;
    
Please Sign in or register to post replies

Write your reply to:

Draft