我在賬戶控制器中添加了自定義索賠。當用戶登錄到系統中時,登錄資料將被添加到索賠中。
這里是代碼,
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)
{
//初始化。
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"/span>, 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(claim, DefaultAuthenticationTypes.ApplicationCookie)。
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
//Sign In.
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, claimIdenties) 。
var identity = new ClaimsIdentity(claim, 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>
添加公司</p>
</a>
</li>
}
uj5u.com熱心網友回復:
你可以使用ViewBag將資料從控制器傳遞到視圖,也可以檢查空值,希望下面的代碼變化會有所幫助。
@{
var value = ((System.Security.Claims.ClaimsIdentity)User.Identity)? .FindFirst("AdminPannel")? .Value。
bool標志。
// if value is "true" or "false".
bool.TryParse(value ? "false", out flag)
}
//現在用flag檢查條件。
if(flag)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/319217.html
標籤:
