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?
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
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
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!
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
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:
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.
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?
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:
This is my current code:
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
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
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
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:
Not anything out of the ordinary, really... Hope this helps you!
Cheers,
Richard
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
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
Yes, I'm certain.
I've added an extra nullcheck just to be sure, which it seems to pass through:
Debugging the member object gives the following:
Perhaps some of these values are generated incorrectly?
Can't see anything wron there, does the CreateAsync call throw an error, or is it returned in the result?
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:
I managed to get an error with a stack trace:
Seems like the functions eventually try to call the Microsoft.AspNetCore.Identity.UserManager
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.
those methods also work for me, so I'm a bit stumped tbh. Maybe some sort of configuration issue.
Probably.
I'll try testing some stuff and see what the problem is. Thanks for your help!
incidentally, you shouldn't need the scopeprovidor in a surface controller, but that is not the problem, just an unnecessary bit of code.
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?
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.
Glad you figured it out.
is working on a reply...