Asked by:
current logged in user details

Question
-
User1897897189 posted
Dears,
I am very new to MVC.
I have following queries and please help me to sort out those.
1. How can i get the current logged in user details in MVC?I have created a new page namely _MainLayout.cshtml and is placed in views/shared
in the HomeController i have added the following;public ActionResult MainLayoutPage()
{
return View();
}I want to display the logged in user details.
How can I do that and where can I do it?
Is it in the Controller page or in the .cshtml file?2.I have a tag like <li id="userdetails">
I want to hide and show the li tag based on loggedin user.
How can i achieve this ?
ThanksMonday, July 15, 2019 5:38 AM
All replies
-
User475983607 posted
1. How can i get the current logged in user details in MVC?User.Identity.Name is the standard but it depends if you are using a standard security pattern.
2.I have a tag like <li id="userdetails">
I want to hide and show the li tag based on loggedin user.
How can i achieve this ?This is standard stuff and Visual Studio has a template that provides this functionality out-of-the-box.
Monday, July 15, 2019 11:19 AM -
User1520731567 posted
Hi nicklibee,
According to your descriptions,I recommend You could create a website based on their starter project in MVC.
Create a new ASP.NET Web project and select the MVC template.
And Leave the default authentication as Individual User Accounts.
Then a project with simple user management features are automatically generated,such as:Register, Login...
After registering or logging in, you can see and optionally add the details of the currently logged in user (user account name, user nickname, etc.).
I have created a new page namely _MainLayout.cshtml and is placed in views/shared
Actually,In above project with Individual User Accounts,It does this for this function with admin panel which created in partial view named _LoginPartial.cshtml:.
Layout:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - My ASP.NET Application</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contact", "Contact", "Home")</li> </ul> @Html.Partial("_LoginPartial") </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - My ASP.NET Application</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
_LoginPartial:
@using Microsoft.AspNet.Identity @if (Request.IsAuthenticated) { using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" })) { @Html.AntiForgeryToken() <ul class="nav navbar-nav navbar-right"> <li>//you could also add other user data,such as: user account name,user nickname,etc. @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" }) </li> <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li> </ul> } } else { <ul class="nav navbar-nav navbar-right"> <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li> <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li> </ul> }
More details,you could refer to this template project.
Hope my reply will helpful to you.
Best Regards.
Yuki Tao
Tuesday, July 16, 2019 4:46 AM