从视图中的声明中读取值

Reading values from claims in view

我在帐户控制器中添加了自定义声明。当用户登录系统时,登录数据将添加到声明中。

这是代码,

public void SignInUser(string username, string userRole, string userid, bool isPersistent, string UserLevel,string AdminPannel, 
            string MasterPannel, string Add_Supplier, string Update_Supplier, string Add_Employee, string Update_Employee,string View_PenReq ,
             string View_ServReq,string View_ReqHis, string View_Inspect, string Authriz_Req, string Sus_CashPay, string Fin_Report)
        {
            // Initialization.    
            var claims = new List<Claim>();
            try
            {
                // Setting    
                claims.Add(new Claim(ClaimTypes.Name, username));
                claims.Add(new Claim(ClaimTypes.Role, userRole));
                claims.Add(new Claim("UserId", userid));
                claims.Add(new Claim("UserLevel", UserLevel));
                claims.Add(new Claim("AdminPannel", AdminPannel));
                claims.Add(new Claim("MasterPannel", MasterPannel));
                claims.Add(new Claim("Add_Supplier", Add_Supplier));
                claims.Add(new Claim("Update_Supplier", Update_Supplier));
                claims.Add(new Claim("Add_Employee", Add_Employee));
                claims.Add(new Claim("Update_Employee", Update_Employee));
                claims.Add(new Claim("View_PenReq", View_PenReq));
                claims.Add(new Claim("View_ServReq", View_ServReq));
                claims.Add(new Claim("View_ReqHis", View_ReqHis));
                claims.Add(new Claim("View_Inspect", View_Inspect));
                claims.Add(new Claim("Authriz_Req", Authriz_Req));
                claims.Add(new Claim("Sus_CashPay", Sus_CashPay));
                claims.Add(new Claim("Fin_Report", Fin_Report));

                TempData["UserLvl"] = UserLevel;


                var claimIdenties = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
                var ctx = Request.GetOwinContext();
                var authenticationManager = ctx.Authentication;
                // Sign In.    
                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, claimIdenties);
                var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
                
                var claimsPrincipal = new ClaimsPrincipal(identity);
                Thread.CurrentPrincipal = claimsPrincipal;
            }
            catch (Exception ex)
            {
                // Info    
                throw ex;
            }
        }

但是在主视图中读取数据时我很吃力。 例如,如果管理面板声明的值为 false,我想从 HTML 视图中隐藏管理面板菜单。我试过了,但没用。你能帮我解决这个问题吗??谢谢

 @if (bool.Parse(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("AdminPannel").Value) != true)
{
  <li class="nav-header">Master Forms</li>
   <li class="nav-item">
    <a href="@Url.Action("Index", "M_Company")" class="nav-link"</a>
      <i class="nav-icon fas fa-table"></i><p>
       Add Company</p>
    </a>
   </li>
}

您可以使用 ViewBag 将数据从控制器传递到视图,还可以检查空值,希望下面的代码更改会有所帮助。声明值以字符串类型返回

            @{
                var value = ((System.Security.Claims.ClaimsIdentity)User.Identity)?.FindFirst("AdminPannel")?.Value;
                bool flag;
                // if value is "true" or "false"
                bool.TryParse(value ?? "false", out flag)
                
             }
            //now check condition with flag

            if(flag)