PasswordSignInAsync Fails, What Next?
# help-with-umbraco
m
I have an Umbraco Form for members to fill out and register on the site. There is a workflow attached which will create a member using the email and password. (The member is created successfully.) And after the form submission, I can use the email address and password to sign in. So I'm trying to save our members a step by signing them in automatically.... and no matter what I try the
IMemberSignInManager
will not work. Do you have any ideas or hints about what may be happening here? The second I go to the sign-in page and use the exact same credentials,s things work! https://cdn.discordapp.com/attachments/1270457524881199296/1270457525082521791/image.png?ex=66b3c545&is=66b273c5&hm=ca250b35027572b081348e83964e3cfa6070b09d17ff151ebd7ce3da0fcef76b& https://cdn.discordapp.com/attachments/1270457524881199296/1270457525325533338/image.png?ex=66b3c545&is=66b273c5&hm=b6b1189a30b84e22a83dae5336d4c645601b6df8371988874667cfd1c7b45a77& https://cdn.discordapp.com/attachments/1270457524881199296/1270457525615067259/image.png?ex=66b3c546&is=66b273c6&hm=1fdee4a543658fd4d13ba0997868d42837743aa8756b513ea2719141981067cb& https://cdn.discordapp.com/attachments/1270457524881199296/1270457525874987018/image.png?ex=66b3c546&is=66b273c6&hm=d387370945aec4bcd93be479dd9f342182375134e368691d26a2d18df3912ffd&
r
Sounds like the member might not be getting saved fully before you try to sign in, could you try using two separate scopes and perhaps adding a short wait between them?
m
Have you closed your scope so the transaction can complete for the creation?
m
Thanks for the reply @Rachel D A few things I tried: - async wait for the member to create - using the "CreateMemberWithIdentity" because the docs mention it persisting before being returned - I put in a large delay (thread.sleep) but I'm not sure if that is a good way of testing Separating the scopes - I will try that next. I assume I can wrap the member creation step in a using statement, and the member sign in statement with a new using statement to accomplish this? (FED if you can't tell. Thanks so much for your help too Matt.)
I tried opening and closing scopes around the two (both are being done as part of the form workflow - create the user, assign the password, and now my attempt to sign in) I tried to use another method which signs in a user with MemberIdentityUser and this at least bubbled up an error. But I happen to be on a Mac and Rider won't let me step into external methods like this. It looks like perhaps there is something "unfinished" with the claims? https://cdn.discordapp.com/attachments/1270457524881199296/1270471150442516631/image.png?ex=66b3d1f6&is=66b28076&hm=9b899774ace1dc32524f5a968f210382fb4dfe599ae9dc8c753d5f2f5577a398& https://cdn.discordapp.com/attachments/1270457524881199296/1270471150748696697/image.png?ex=66b3d1f6&is=66b28076&hm=c15ecf1a9aec21b65cf4beb7393eb2e6c20693cab8c551fe3a7ee53d9d6e9ea3& https://cdn.discordapp.com/attachments/1270457524881199296/1270471151100891266/image.png?ex=66b3d1f6&is=66b28076&hm=e30d793578c6a25358e3dda865381bdcb996644c93a9ab872df2673605be9537&
To fix my issue I did the following: - Replaced
_memberService.CreateMemberWithIdentity
with
MemberIdentityUser.CreateNew
- Used
_memberManager.CreateAsync
with the result of the above
CreateNew
Everything magically works if you do it in this order. If you try to use the member service to create everything all at once, it seems like a race condition exists.
This also solves some sillyness I had to do to request a password reset token to set the actual password. All around improvements.
Copy code
var identityUser = MemberIdentityUser.CreateNew(
            email, 
            email, 
            memberType.Alias, 
            true,
            $"{firstName} {lastName}");
        var identityResult = await _memberManager.CreateAsync(identityUser, password);

        if (identityResult.Succeeded)
        {
            IMember? member = _memberService.GetByKey(identityUser.Key);
            member.SetValue("firstName", firstName);
            member.SetValue("lastName", lastName);
            _memberService.Save(member);
                        
            await _signInManager.SignInAsync(identityUser, false);
        }
50 Views