如果 User.IsInRole 带有字符串数组?

发布于 2024-08-19 11:35:50 字数 1003 浏览 7 评论 0原文

我正在尝试构建自定义验证,在其中检查角色是否包含用户。我在使用字符串数组时遇到问题,检查它是否包含特定值的最佳方法是什么?

    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 的检查以便它处理数组?

Im trying to build a custom validation where I check if the role contains a user. And I'm having problems with string array, what is best way to check if it contains a specific value?

    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");
        }

How should I modify the check for User.IsInRole so it handles the array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑色毁心梦 2024-08-26 11:35:50

怎么样:

bool isAuthorized = 
    this.AuthRoles.Any(r => filterContext.HttpContext.User.IsInRole(r));

编辑:(假设成为任何角色的成员就足以获得授权。)

How about:

bool isAuthorized = 
    this.AuthRoles.Any(r => filterContext.HttpContext.User.IsInRole(r));

Edit: (Assuming that being member of any of the roles is enough to be authorized.)

小霸王臭丫头 2024-08-26 11:35:50

如果您希望用户同时拥有 AuthRoles 中的所有角色,您应该:

bool isAuthorized =
         Array.TrueForAll(AuthRoles, filterContext.HttpContext.User.IsInRole);

如果仅成为至少一个所需角色的成员就足够了,请使用 Any< /代码>:

bool isAuthorized = AuthRoles.Any(filterContext.HttpContext.User.IsInRole);

If you want the user to have all the roles in the AuthRoles at the same time, you should:

bool isAuthorized =
         Array.TrueForAll(AuthRoles, filterContext.HttpContext.User.IsInRole);

If just being a member of at least one of the required roles is enough, use Any:

bool isAuthorized = AuthRoles.Any(filterContext.HttpContext.User.IsInRole);
在风中等你 2024-08-26 11:35:50

您可以使用简单的 linq 表达式来完成此操作:

bool isAuthorized = AuthRoles.All(filterContext.HttpContext.User.IsInRole);

You can do it with a simple linq expression:

bool isAuthorized = AuthRoles.All(filterContext.HttpContext.User.IsInRole);
药祭#氼 2024-08-26 11:35:50

您需要检查每个字符串

bool isAuthorized = false;

foreach(string role in AuthRoles)
{
  if(filterContext.HttpContext.User.IsInRole(role))
    isAuthorized = true;
}

You need to check each string

bool isAuthorized = false;

foreach(string role in AuthRoles)
{
  if(filterContext.HttpContext.User.IsInRole(role))
    isAuthorized = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文