Hi all. I Have an page i want to use a left menu.
When I use regular render, I cant get user info.
Header
Left menu
Here i need info of loged in user +++
body
Footer
My purpose of this is that Left side menu stores the logged in user details.
In body section, the page will be different. example, customers.chtml, orders, cshtml and so on.
Anyone have a opinion of this?
I've wrote something when I wrote classic asp code.
Seriously. How do you expect the MVC community to help you if you wrote a custom authentication/authorization? We cannot see your custom code and have no idea how code works.
cbrAndy
I should probably use the mvc script.
I recommend using standard ASP.NET security. You have to read documentation for the framework you are using ASP.NET or Core.
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
In my _leftMenu i got the information by doing this:
var myEmployee = ViewBag.myEmployee;
var myCompany = ViewBag.myCompany;
var myRole = ViewBag.myRole;
Which i coult post like this in layout view:
var name = myEmployee.EmployeeFirstName + " " + myEmployee.EmployeeLastname;
This works fine! Nothing problem here untill i press a link, that goes to another controller.
Then the viewbag is empty and nothing works longer.
So I'm back to start.
Basicly what I'm trying to do is to have a crm page.
Main page after login, i'm getting to a dashboard.
Left menu I can choose several menus. The menu is not same for all user.
Pressing on a link, the main menu is changed as the link you provided.
Do you have a clue how I can do this?
This works fine! Nothing problem here untill i press a link, that goes to another controller.
Again, if you follow standard MVC security patterns then this is a non-issue. User information like name, company, roles, etc are typically stored in claims and cached in an authentication cookie when the user logs in. The following link illustrates how
to cache claims in an authentication cookie without Identity.
Hello mgebhard.
Ok, so this is my case.
I have in my left menu, projects, customers, sales. This comes from a database, which each loged in user can only see.
So if I use MVC security patterns i'll get these? just like reading a viewbag?
Similar but not the same. The user information is stored in claims. The claims are cached in the authentication cookie as illustrated in the links provided above. It is just a mater of fetching the claims which is very simple.
This was not a simple quick fix. I need to do all my code again.
Or do you know a better way to do this?
Sometimes initial designs do not work out as expected. You built a custom solution that must populate the ViewBag on each and every request. If you want to continue using your solution then it is up to you to come up with a design. Perhaps a custom attribute
or a base controller.
I simply explained how to interface with MVC security by caching user claims in the standard authentication cookie. This approach also allows you to use the [Authorize] attribute.
hehe...
I'm actually doing the hard way and trying to understand the MVC security. It will take time, but first I'm trying to do the leftside menu.
I'm going to make an @Html.Partial("_Sidemenu") for this.
For this I'm now try to make an controller anv view model for this. Is this the correct way to do this?
According to your needs, I wrote an example, you can refer to it.
I suggest you use Identity to achieve authentication.
The following example is modified in the ASP.NET MVC Identitytemplate project, you can modify it according to your own needs.
You need to modify the _Layout view, then create a Menu
class, and implement dynamic menus based on roles.
ApplicationUser
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
ApplicationDbContext context = new ApplicationDbContext();
var roleManager = new RoleManager<ApplicationUserRole>(new RoleStore<ApplicationUserRole>(context));
var role = manager.GetRoles(userIdentity.GetUserId())[0].ToString();
var roleid = roleManager.Roles.Where(m => m.Name == role).Select(m => m.Id).FirstOrDefault();
var menulist = context.Menus.Include("ApplicationUserRoles").Include("SubMenus").Where(m => m.ApplicationUserRoles.Any(i => i.Id == roleid)).ToList();
var menulistjsonstring = JsonConvert.SerializeObject(menulist, Formatting.Indented,
new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
userIdentity.AddClaim(new Claim("menulist", menulistjsonstring));
return userIdentity;
}
}
public class ApplicationUserRole: IdentityRole
{
public List<Menus> Menus { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Menus> Menus { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>()
.HasMany(m=>m.Menus)
.WithMany(m=>m.ApplicationUserRoles).Map(cs =>
{
cs.MapLeftKey("RoleId");
cs.MapRightKey("MainMenuId");
cs.ToTable("AspNetRolesMenus");
});
}
}
Menus
[JsonObject(IsReference = true)]
public class Menus
{
[Key]
public int MainMenuId { get; set; }
public string MainMenuName { get; set; }
public string MainMenuUrl { get; set; }
public int? ParentMenuId { get; set; }
[ForeignKey("ParentMenuId")]
public List<Menus> SubMenus { get; set; }
public List<ApplicationUserRole> ApplicationUserRoles { get; set; }
}
public static class IdentityExtensions
{
public static List<Menus> GetMenusList(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var claimsidentity = identity as ClaimsIdentity;
List<Menus> menulist = new List<Menus>();
if (claimsidentity != null)
{
var value = claimsidentity.FindFirstValue("menulist");
menulist = String.IsNullOrEmpty(value)?null: JsonConvert.DeserializeObject<List<Menus>>(value);
}
return menulist;
}
}
Here is the result.
Best Regards,
YihuiSun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
2 Points
17 Posts
Left side Menu with user info
Mar 15, 2021 12:51 PM|cbrAndy|LINK
Hi all. I Have an page i want to use a left menu.
When I use regular render, I cant get user info.
Here i need info of loged in user +++
My purpose of this is that Left side menu stores the logged in user details.
In body section, the page will be different. example, customers.chtml, orders, cshtml and so on.
Anyone have a opinion of this?
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Mar 15, 2021 01:03 PM|mgebhard|LINK
The user principal is very easy to get to if you are using standard authentication/authorization that comes with MVC.
The standard MVC 5 templates have sample code (_loginPartial.cshtml) that shows how to display user information in the main layout.
At this point it is not clear why you are unable to get user to the user information or what user information you wish to show in the left menu.
Member
2 Points
17 Posts
Re: Left side Menu with user info
Mar 15, 2021 09:07 PM|cbrAndy|LINK
I have not used the mvc authorization code
I've wrote something when I wrote classic asp code.
I should probably use the mvc script.
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Mar 15, 2021 10:02 PM|mgebhard|LINK
Seriously. How do you expect the MVC community to help you if you wrote a custom authentication/authorization? We cannot see your custom code and have no idea how code works.
I recommend using standard ASP.NET security. You have to read documentation for the framework you are using ASP.NET or Core.
https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/introduction-to-aspnet-identity
Contributor
3070 Points
873 Posts
Re: Left side Menu with user info
Mar 16, 2021 09:20 AM|YihuiSun|LINK
Hi cbrAndy,
You can modify your layout view to achieve your needs.
Best Regards,
YihuiSun
Member
2 Points
17 Posts
Re: Left side Menu with user info
Mar 26, 2021 04:34 PM|cbrAndy|LINK
Hello YihuiSun.
I've been testing some today.
I really can't get a grip for this.
I made this in my CRMcontroller
In my _leftMenu i got the information by doing this:
Which i coult post like this in layout view:
This works fine! Nothing problem here untill i press a link, that goes to another controller.
Then the viewbag is empty and nothing works longer.
So I'm back to start.
Basicly what I'm trying to do is to have a crm page.
Main page after login, i'm getting to a dashboard.
Left menu I can choose several menus. The menu is not same for all user.
Pressing on a link, the main menu is changed as the link you provided.
Do you have a clue how I can do this?
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Mar 26, 2021 04:48 PM|mgebhard|LINK
Again, if you follow standard MVC security patterns then this is a non-issue. User information like name, company, roles, etc are typically stored in claims and cached in an authentication cookie when the user logs in. The following link illustrates how to cache claims in an authentication cookie without Identity.
Core MVC
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-5.0
ASP.NET MVC
https://stackoverflow.com/questions/31511386/owin-cookie-authentication-without-asp-net-identity
Data driven menus are a very common web app feature. Do a internet search for examples. Typically the menu is cached for fast look ups.
https://www.google.com/search?q=mvc+Data+driven+menu
Member
2 Points
17 Posts
Re: Left side Menu with user info
Mar 26, 2021 05:18 PM|cbrAndy|LINK
Hello mgebhard.
Ok, so this is my case.
I have in my left menu, projects, customers, sales. This comes from a database, which each loged in user can only see.
So if I use MVC security patterns i'll get these? just like reading a viewbag?
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Mar 26, 2021 07:01 PM|mgebhard|LINK
Similar but not the same. The user information is stored in claims. The claims are cached in the authentication cookie as illustrated in the links provided above. It is just a mater of fetching the claims which is very simple.
Member
2 Points
17 Posts
Re: Left side Menu with user info
Mar 26, 2021 10:17 PM|cbrAndy|LINK
Ok. I'll try to i plent this tomorrow. Thanks for your tips
Member
2 Points
17 Posts
Re: Left side Menu with user info
Apr 01, 2021 09:08 AM|cbrAndy|LINK
This was not a simple quick fix. I need to do all my code again.
Or do you know a better way to do this?
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Apr 01, 2021 10:13 AM|mgebhard|LINK
Sometimes initial designs do not work out as expected. You built a custom solution that must populate the ViewBag on each and every request. If you want to continue using your solution then it is up to you to come up with a design. Perhaps a custom attribute or a base controller.
I simply explained how to interface with MVC security by caching user claims in the standard authentication cookie. This approach also allows you to use the [Authorize] attribute.
Member
2 Points
17 Posts
Re: Left side Menu with user info
Apr 02, 2021 08:14 AM|cbrAndy|LINK
hehe...
I'm actually doing the hard way and trying to understand the MVC security. It will take time, but first I'm trying to do the leftside menu.
I'm going to make an @Html.Partial("_Sidemenu") for this.
For this I'm now try to make an controller anv view model for this. Is this the correct way to do this?
-André
All-Star
53721 Points
24052 Posts
Re: Left side Menu with user info
Apr 02, 2021 10:56 AM|mgebhard|LINK
No. Good luck.
Contributor
3070 Points
873 Posts
Re: Left side Menu with user info
Apr 02, 2021 11:42 AM|YihuiSun|LINK
Hi cbrAndy,
According to your needs, I wrote an example, you can refer to it.
ApplicationUser
Menus
_Layout.cshtml
_LeftMenu
IdentityExtensions
Here is the result.
Best Regards,
YihuiSun
Member
2 Points
17 Posts
Re: Left side Menu with user info
Apr 06, 2021 08:06 AM|cbrAndy|LINK
YihuiSun.
Again, I need to take a bow for you.
Thank you so much for your help.