我試圖建立一個自定義驗證,我檢查角色是否包含用戶。而且我遇到了字符串數組的問題,檢查它是否包含特定值的最佳方法是什麼?如果User.IsInRole與字符串數組?
public string[] AuthRoles { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (AuthRoles.Length > 0)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
RedirectToRoute(filterContext,
new
{
controller = "AdminLogin",
action = "AdminLogin"
});
}
else
{
bool isAuthorized = filterContext.HttpContext.User.IsInRole(this.AuthRoles.??);
if (!isAuthorized)
throw new UnauthorizedAccessException("You are not authorized to view this page");
}
}
else
{
throw new InvalidOperationException("No Role Specified");
}
我應該如何修改User.IsInRole檢查,因此處理陣列?
我的解決方案只需要用戶是角色之一的成員。所以這取決於你想要的東西。 – AaronLS