3
在ASP.NET MVC標識中,用戶和角色的關係數據保存在AspNetUserRoles表中,該表有兩個字段:UserId,RoleId,但是我想添加該表中的其他字段,例如部門字段。ASP.NET MVC標識:如何擴展AspNetUserRoles表
因此,如果用戶登錄不同的部門,他將有不同的角色。 任何人都知道如何做到這一點?提前致謝!
在ASP.NET MVC標識中,用戶和角色的關係數據保存在AspNetUserRoles表中,該表有兩個字段:UserId,RoleId,但是我想添加該表中的其他字段,例如部門字段。ASP.NET MVC標識:如何擴展AspNetUserRoles表
因此,如果用戶登錄不同的部門,他將有不同的角色。 任何人都知道如何做到這一點?提前致謝!
我建議您調查ASPNet用戶聲明。您可以使用身份管理器爲用戶分配不同的聲明,並根據用戶的聲明類型,允許他訪問或不訪問。創建一個自定義聲明屬性,該屬性將放置在各種控制器的頂部以驗證用戶身份。這必須根據您的需求來實施。自定義屬性將在控制器執行前觸發,如果允許使用,他將通過。否則返回到你選擇的錯誤頁面。
樣本屬性使用
[ClaimsAuthorize(ClaimsData.EditAddress)]
public ActionResult CitiesPartial()
屬性認證
public class ClaimsAuthorizeAttribute : AuthorizeAttribute
{
private readonly string _claimType;
public ClaimsAuthorizeAttribute(string type)
{
_claimType = type;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
var user = (ClaimsPrincipal)HttpContext.Current.User;
if (user.HasClaim(_claimType, "True"))
{
base.OnAuthorization(filterContext);
}
else
{
HandleUnauthorizedRequest(filterContext, _claimType + " Not Allowed ");
}
}
protected void HandleUnauthorizedRequest(AuthorizationContext filterContext, string message)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "ClaimNotAuthorized" },
{ "controller", "Home" },
{"errorMessage", message }
});
}
public static bool AuthorizedFor(string claimType)
{
var user = (ClaimsPrincipal)HttpContext.Current.User;
return user.HasClaim(claimType, "True");
}
}
希望這會有所幫助。
如果向** AspNetUserRoles **表添加一列,則需要進行大量基礎結構更改(包括過濾器)。最簡單的方法是用**部門 - 角色名稱**添加角色。例如,*人力資源部門 - 經理*。 – Win
或使用UserId RoleId DepartmentId – user5103147
創建您自己的表不知道如何使用EF代碼首先建模3鍵表。任何幫助,將不勝感激。 – Frank