Copied to clipboard

Flag this post as spam?

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


  • Cornelis 12 posts 113 karma points
    May 08, 2020 @ 12:18
    Cornelis
    0

    Intercepting the save member throws a nullReferenceException

    For our web application the functionality description demands the end-user to login with an e-mail address instead of an username. Unfortunately, MembershipHelper doesn't provide a method to login with an e-mail address/password (Why not?), only username/password.

    In the search adventure of figuring out how to login with e-mail/password, I found this topic: https://our.umbraco.com/forum/using-umbraco-and-getting-started/86635-retrieve-a-members-login-by-their-email-address

    Here the username is retrieved via the user Id via the e-mail address. This looks like needing 2 extra calls to the database which we don't like and also makes the code less pretty and is.

    Another solution that we found is using an intercept acting solution. As this solution introduces a sperate piece of code and no extra database calls, we decided to choose this solution as it is in the code below:

    public class SaveMemberInterceptor : IComponent
    {
        public void Initialize()
        {
            MemberService.Saving += SetUserNameEqualToEmailAddress;
        }
    
        public void Terminate()
        {
            //Nothing to terminate
        }
    
        private void SetUserNameEqualToEmailAddress(IMemberService sender, SaveEventArgs<IMember> saveEventArguments) 
        {
            foreach (var entity in saveEventArguments.SavedEntities)
            {
                entity.Username = entity.Email;
            }
        }
    }
    
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class SubscribeToMediaSavedEventComposer : ComponentComposer<SaveMemberInterceptor>
    {
    }
    

    Unfortunately, this code throws runtime exceptions:

    Received an error from the server An error occured Object reference not set to an instance of an object.

    Exception Details System.NullReferenceException: Object reference not set to an instance of an object. Stacktrace

    at Umbraco.Web.Editors.MemberController.UpdateName(MemberSave memberSave) in D:\a\1\s\src\Umbraco.Web\Editors\MemberController.cs:line 614 at Umbraco.Web.Editors.MemberController.RefetchMemberData(MemberSave contentItem, LookupType lookup) in D:\a\1\s\src\Umbraco.Web\Editors\MemberController.cs:line 586 at Umbraco.Web.Editors.MemberController.CreateWithMembershipProvider(MemberSave contentItem, MembershipCreateStatus& status) in D:\a\1\s\src\Umbraco.Web\Editors\MemberController.cs:line 716 at Umbraco.Web.Editors.MemberController.PostSave(MemberSave contentItem) in D:\a\1\s\src\Umbraco.Web\Editors\MemberController.cs:line 318 at lambdamethod(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>cDisplayClass62.

    We might be using Umbraco 7 code in Umbraco 8 project but one thing we noticed: the documentation for Umbraco 8 for this subject is very poor (or we are using the wrong terms while searching, also possible of course).

    What are we doing wrong and what needs to be done to fix the issue?

  • Simon 1 post 71 karma points
    Sep 30, 2020 @ 16:57
    Simon
    0

    Hi,

    Did you find a solution for this at all? I have a similar issue. I have been following an article about encryption (https://skrift.io/issues/encryption-injection-in-v8/) but get the same error when trying to encrypt the username using the saving event.

    Thanks

  • Cornelis 12 posts 113 karma points
    Oct 01, 2020 @ 07:56
    Cornelis
    0

    Unfortunately we haven't found a solution for this problem yet. Currently we solved it by teaching the customer not to enter a normal username in the username field but fill in an e-mail address instead. Not the best solution, but currently it seems to be the only one there is.

Please Sign in or register to post replies

Write your reply to:

Draft