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?
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:
And I am attempting to access the current user later in a Change Password controller that looks as follows:
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?
And if you check if there's a User logged in? I find the parity of a Users and a Members confusing.
Did you find a solution for this?
In our case, it appeared that Umbraco Basic Authentication breaks GetCurrentMemberAsync, so we just disabled it
is working on a reply...