I added custom claims in the account controller. When the user logged in to the system login data will be added to the claims.
Here is the code,
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;
}
}
But I'm struggling when reading the data in the main view. For example, if the value of the claim for the Admin Panel is false, I want to hide the Admin panel menu from the HTML view. I tried this and it won't work. Can you help me with this ?? Thanks
@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>
}
CodePudding user response:
you can use ViewBag to pass data from controller to view, also check for nulls, hope below code change will help. claims values are returned in string type
@{
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)