Copied to clipboard

Flag this post as spam?

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


  • Isaac Meyers 8 posts 88 karma points
    Jun 14, 2023 @ 21:46
    Isaac Meyers
    0

    MemberManager.IsLoggedIn returns true, but MemberManager.GetCurrentMemberAsync returns null.

    Hi there,

    I am currently setting up a login page for a "Member only" type page on my site.

    However, I am having a problem when trying to access _memberManager.GetCurrentMemberAsync();

    My Auth controller looks like this:

    [HttpPost]
        public async Task<IActionResult> SubmitLogin(MemberLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Invalid Model!");
                return CurrentUmbracoPage();
            }
            if (!User.Identity.IsAuthenticated)
            {
    
                var isValid = await _memberManager.ValidateCredentialsAsync(model.Username, model.Password);
                if (isValid)
                {
                    MemberIdentityUser thisUser = await _memberManager.FindByNameAsync(model.Username);
                    await _memberSignInManager.SignInAsync(thisUser, true);
    
                    var claims = new List<Claim>();
                        claims.Add(new Claim(ClaimTypes.Name, model.Username));
                        var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                        var principal = new ClaimsPrincipal(identity);
    
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(principal));
                    return Redirect("/partner-portal/");
                }
            }
    
            return CurrentUmbracoPage();
        }
    

    And I am attempting to access the current user later in a Change Password controller that looks as follows:

    [HttpPost]
        public async Task<IActionResult> SubmitChangeMemberPassword(MemberPasswordChangeModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                var currentLogged = _memberManager.IsLoggedIn();
                var currentMember = await _memberManager.GetCurrentMemberAsync();
    
                //Need to verify the current user == the user passed in the model
                //Otherwise a user could change another users settings :)
                // Check if the email address is in the database
                if (currentMember == null)
                {
                    TempData["error_message"] = "Sorry, we could not update your password.";
                    return CurrentUmbracoPage();
                }
    
                if (model.NewPassword.Length < 8 ||
                    model.NewPasswordRepeat.Length < 8)
                {
                    TempData["error_message"] = "Password must be at least 8 characters in length.";
                    return CurrentUmbracoPage();
                }
    
                if (model.NewPassword != model.NewPasswordRepeat)
                {
                    TempData["error_message"] = "Passwords do not match. Please check your new password is typed correctly in both fields and try again.";
                    return CurrentUmbracoPage();
                }
                else
                {
                    TempData["success_message"] = "Your password was successfully changed.";
                    this._memberManager.ChangePasswordAsync(currentMember, "", model.NewPassword);
                }
            }
            else
            {
                //log out the user
                MemberAuthController mac = new MemberAuthController(
                    this._umbracoContextAccessor,
                    this._databaseFactory,
                    this._services, this._appCaches,
                    this._profilingLogger,
                    this._publishedUrlProvider,
                    this._memberManager,
                    this._memberSignInManager
                );
                mac.SubmitLogout();
            }
            return CurrentUmbracoPage();
        }
    

    When I hit a breakpoint in the ChangeMemberPasswork controller, I can see that _memberManager.IsLoggedIn() is true, however, _memberManager.GetCurrentMemberAsync(); returns null.

    If the user is logged in, then I would assume that there is a current member to get, no?

  • paul 11 posts 101 karma points
    Jun 15, 2023 @ 13:24
    paul
    0

    And if you check if there's a User logged in? I find the parity of a Users and a Members confusing.

  • Mads Justesen 9 posts 40 karma points
    Oct 24, 2023 @ 08:12
    Mads Justesen
    0

    Did you find a solution for this?

  • loremIpsumQwe 1 post 71 karma points
    1 day ago
    loremIpsumQwe
    0

    In our case, it appeared that Umbraco Basic Authentication breaks GetCurrentMemberAsync, so we just disabled it

Please Sign in or register to post replies

Write your reply to:

Draft