C# 基于角色的动态左侧菜单(使用ASP .NET Identity)

实现一个基于用户角色的左侧栏菜单

  1. 创建一个ASP .NET Identity的模板项目。
  2. 修改模板项目的_Layout视图,实现左侧菜单。
  3. 创建一个Menu的class用存储菜单信息,并且创建ApplicationUserRole(让它继承IdentityRole),并且配置Menu和ApplicationUserRole之间多对多的关系。
  4. 可以点击链接去下载完整的项目,也可以直接查看下面的代码(只粘贴了部分)。
  5. 有时间我会慢慢加上注释的。

Menus

using Microsoft.AspNet.Identity.EntityFramework;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace DynamicLeftMenuBasedRoleMVCIdentityDemo.Models
{
    [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; }
    }
}

IdentityModels

using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Newtonsoft.Json;

namespace DynamicLeftMenuBasedRoleMVCIdentityDemo.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    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));
            // Add custom user claims here
            return userIdentity;
        }
    }
    publ
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值