Hi all.
I have a problem i solved for 2 years ago. But now i can't figure it out.
I have a page with a side menu. This side menu is the same when you are logged in. Here there is your full name, your position, companyname etc. But how can i do this? Since I created a new project in VS 2022, I now use .net 7 and I can't access the Area account controller, so i had to create an Account controller.
Account.cs
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
// Get the user
var user = await _userManager.FindByEmailAsync(model.Email);
if (user != null)
{
// Retrieve full name and position
string fullName = user.FullName;
var myCompany = _context.Company.Find(user.CompanyID);
var myPosition = _context.Position.Where(p => p.PositionId == user.PositionId).SingleOrDefault();
var myDepartment = _context.Department.Where(p => p.DepartmentId == user.DepartmentId).SingleOrDefault();
Viewbag.PositionName = myPosition.PositionName;
Viewbag.CompanyName = myCompany.CompanyName;
Viewbag.DepartmentName = myDepartment.DepartmentName;
if (string.IsNullOrEmpty(returnUrl))
{
// returnUrl is null or empty
return RedirectToAction("Index", "Home");
}
else
{
// returnUrl is not null or empty
return LocalRedirect(returnUrl);
}
}
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
}
TempData["Message"] = "Something fault happened during Login!";
// If we got this far, something failed, redisplay the form
return View(model);
}
_Loggedin.cshtml
<div class="page-wrapper chiller-theme toggled">
@await Html.PartialAsync("_Sidemenu")
<!-- page-content" -->
<main class="page-content">
@RenderBody()
<footer class="footer">
test
</footer>
</main>
<!-- page-content" -->
</div>
_sidemenu.cshtml
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@{
// Get the current logged-in user
var user = await UserManager.GetUserAsync(User);
var userloggedin = user;
string fullName = user?.FullName;
string positionName = viewbag.PositionName
}
This doesn't work!
Can anyone please help?