Copied to clipboard

Flag this post as spam?

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


  • GunsRoseSS 12 posts 82 karma points
    Mar 08, 2023 @ 10:01
    GunsRoseSS
    0

    IMemberManager.CreateAsync() returns error despite receiving a valid member

    Hi all,

    I've been trying to implement a member registration page in Umbraco 10.4.0 by following the official Umbraco documentation, but I can't seem to create a member. The CreateAsync() function always returns the following error:

    "IdentityErrorUserStore": "One or more errors occurred. (One or more errors occurred. (Value cannot be null. (Parameter 'user')))"
    

    This is my current code:

    [HttpPost]
        [ValidateUmbracoFormRouteString]
        public async Task<IActionResult> HandleRegisterMember([Bind(Prefix = "RegisterModel")] RegisterModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.CurrentUmbracoPage();
            }
    
            using var scope = coreScopeProvider.CreateCoreScope(autoComplete: true);
    
            var member = MemberIdentityUser.CreateNew(model.UsernameIsEmail ? model.Email : model.Username ?? model.Email,
                                                        model.Email, model.MemberTypeAlias, false, model.Name);
            var result = await memberManager.CreateAsync(member, model.Password);
    
           // Handle result..
        }
    

    I've checked the member variable that is created, and I don't see anything particularly odd about it... Does anyone have an idea as to why I'm receiving this error?

    Cheers,

    Richard

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 08, 2023 @ 12:44
    Huw Reddick
    0

    Mmm, your code looks identical to mine TBH so not sure what is happening, I will give mine a quick run through to ensure it still working though and get back to you later. My reg code may be just umbraco 10.0 though so will check

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 08, 2023 @ 13:35
    Huw Reddick
    0

    mmm, works fine on my 10.4 site, not very helpful I know. Could you maybe provide more code from your controller and I will see if I can replicate your problem

  • GunsRoseSS 12 posts 82 karma points
    Mar 08, 2023 @ 14:01
    GunsRoseSS
    0

    Hi Huw,

    Thanks for checking. I've also tried downgrading my site to v10.0, but I still got the same error...

    This is my SurfaceController:

    using Microsoft.AspNetCore.Mvc;
    using Umbraco.Cms.Core.Cache;
    using Umbraco.Cms.Core.Logging;
    using Umbraco.Cms.Core.Routing;
    using Umbraco.Cms.Core.Scoping;
    using Umbraco.Cms.Core.Security;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Cms.Infrastructure.Persistence;
    using Umbraco.Cms.Web.Common.Filters;
    using Umbraco.Cms.Web.Website.Controllers;
    using Umbraco.Cms.Web.Website.Models;
    
    namespace MyProject.Code.Controllers.Surface
    {
        /// <summary>
        /// SurfaceController responsible for handling all member related forms.
        /// </summary>
        public class MemberSurfaceController : SurfaceController
        {
            private readonly IMemberManager memberManager;
            private readonly ICoreScopeProvider coreScopeProvider;
    
            public MemberSurfaceController(IUmbracoContextAccessor umbracoContextAccessor,
                IUmbracoDatabaseFactory databaseFactory,
                ServiceContext services,
                AppCaches appCaches,
                IProfilingLogger profilingLogger,
                IPublishedUrlProvider publishedUrlProvider,
                IMemberManager memberManager,
                ICoreScopeProvider coreScopeProvider)
                : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
            {
                this.memberManager = memberManager;
                this.coreScopeProvider = coreScopeProvider;
            }
    
            [HttpPost]
            [ValidateUmbracoFormRouteString]
            public async Task<IActionResult> HandleRegisterMember([Bind(Prefix = "registerModel")] RegisterModel model)
            {
                if (!this.ModelState.IsValid)
                {
                    return this.CurrentUmbracoPage();
                }
    
                using var scope = coreScopeProvider.CreateCoreScope(autoComplete: true);
    
                var member = MemberIdentityUser.CreateNew(model.UsernameIsEmail ? model.Email : model.Username ?? model.Email,
                                                            model.Email, model.MemberTypeAlias, false, model.Name);
                var result = await memberManager.CreateAsync(member, model.Password);
    
                if (result.Succeeded)
                {
                    // This part never gets called, so I removed it for clarity.
                }
    
                return this.CurrentUmbracoPage();
            }
        }
    }
    

    Not anything out of the ordinary, really... Hope this helps you!

    Cheers,

    Richard

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 08, 2023 @ 15:46
    Huw Reddick
    0

    I will do some testing with your code snippet and see if I can replicate the error, I tested on both 10.0 and 10.4 without a problem just using some dummy code. Will let you know result of trying your code later

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 09, 2023 @ 09:20
    Huw Reddick
    0

    Are you certain that member has a value? The only way I could replicate your error was by actually passing a null value to CreateAsync method

  • GunsRoseSS 12 posts 82 karma points
    Mar 09, 2023 @ 11:37
    GunsRoseSS
    0

    Yes, I'm certain.

    I've added an extra nullcheck just to be sure, which it seems to pass through:

    enter image description here

    Debugging the member object gives the following:

    enter image description here

    Perhaps some of these values are generated incorrectly?

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 09, 2023 @ 12:36
    Huw Reddick
    0

    Can't see anything wron there, does the CreateAsync call throw an error, or is it returned in the result?

  • GunsRoseSS 12 posts 82 karma points
    Mar 09, 2023 @ 13:46
    GunsRoseSS
    0

    It's returned as a result.

    However, I've noticed that this error also occurs in similar methods in the IMemberService interface. After implementing this code:

    var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, model.MemberTypeAlias);
                memberService.Save(member);
    

    I managed to get an error with a stack trace:

    enter image description here

    Seems like the functions eventually try to call the Microsoft.AspNetCore.Identity.UserManager

    /// <summary>
            /// Gets a list of role names the specified <paramref name="user"/> belongs to.
            /// </summary>
            /// <param name="user">The user whose role names to retrieve.</param>
            /// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing a list of role names.</returns>
            public virtual async Task<IList<string>> GetRolesAsync(TUser user)
            {
                ThrowIfDisposed();
                var userRoleStore = GetUserRoleStore();
                if (user == null)
                {
                    throw new ArgumentNullException(nameof(user));
                }
                return await userRoleStore.GetRolesAsync(user, CancellationToken);
            }
    

    Which is strange, it shouldn't have to call this function when simply creating a member. Any ideas?

    PS: IMemberService.CreateMember results in the same error.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 09, 2023 @ 14:03
    Huw Reddick
    0

    those methods also work for me, so I'm a bit stumped tbh. Maybe some sort of configuration issue.

  • GunsRoseSS 12 posts 82 karma points
    Mar 10, 2023 @ 08:50
    GunsRoseSS
    0

    Probably.

    I'll try testing some stuff and see what the problem is. Thanks for your help!

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 08, 2023 @ 15:48
    Huw Reddick
    0

    incidentally, you shouldn't need the scopeprovidor in a surface controller, but that is not the problem, just an unnecessary bit of code.

  • Tor Langlo 189 posts 532 karma points
    Mar 08, 2023 @ 21:18
    Tor Langlo
    0

    A full stack trace for the thrown exception would be helpful. Maybe the content of one of the parameters to the CreateNew method isn't what you excpect it to be?

  • GunsRoseSS 12 posts 82 karma points
    Mar 13, 2023 @ 07:43
    GunsRoseSS
    100

    Turns out the exception didn't have to do with my SurfaceController at all... It just initiated it.

    I had a MemberSavingNotification handler that was fired and called the GetRolesAsync() function. But I was missing a nullcheck... I feel so dumb.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Mar 13, 2023 @ 09:00
    Huw Reddick
    0

    Glad you figured it out.

Please Sign in or register to post replies

Write your reply to:

Draft