AuthorizeAttribute 构造函数参数 Roles equals string.empty 含义?

发布于 2024-12-21 20:27:16 字数 378 浏览 4 评论 0原文

这是一些 MCTS 70-515 考试练习测试中的问题。

请帮助提供正确的 2 个答案


您正在实现一个 ASP.NET MVC 2 Web 应用程序,该应用程序允许用户查看和编辑数据。您需要确保只有登录用户才能访问控制器的编辑操作。您可以添加到“编辑”操作以实现此目标的两个可能属性是什么?

(每个正确答案都提供一个完整的解决方案。选择两个。)

  1. [Authorize(Users = "")]
  2. [Authorize(Roles = "")]
  3. [Authorize(Users = "*")]
  4. [Authorize(Roles = "*") ]

this a question from some MCTS 70-515 Exam Practice tests.

please help with the correct 2 answers


You are implementing an ASP.NET MVC 2 Web application that allows users to view and edit data. You need to ensure that only logged-in users can access the Edit action of the controller. What are two possible attributes that you can add to the Edit action to achieve this goal?

(Each correct answer presents a complete solution. Choose two.)

  1. [Authorize(Users = "")]
  2. [Authorize(Roles = "")]
  3. [Authorize(Users = "*")]
  4. [Authorize(Roles = "*")]

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

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

发布评论

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

评论(2

拧巴小姐 2024-12-28 20:27:16

查看AuthorizeAttribute的源代码发现没有通配符“*”。

如果 [Authorize(Users = "")] 会导致“没有人”可以访问该操作,则毫无意义。

所以答案12是正确的。

AuthorizeAttribute 的源代码

protected virtual bool AuthorizeCore(HttpContextBase httpContext) {
    if (httpContext == null) {
        throw new ArgumentNullException("httpContext");
    }

    IPrincipal user = httpContext.User;
    if (!user.Identity.IsAuthenticated) {
        return false;
    }

    if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) {
        return false;
    }

    if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) {
        return false;
    }

    return true;
}

以及RoleUsers 属性。

public string Roles {
    get {
        return _roles ?? String.Empty;
    }
    set {
        _roles = value;
        _rolesSplit = SplitString(value); // simple split by comma
    }
}

public string Users {
    get {
        return _users ?? String.Empty;
    }
    set {
        _users = value;
        _usersSplit = SplitString(value); // simple split by comma
    }
}

A look at the source code of the AuthorizeAttribute shows that there is no wildcard "*".

It makes no sense if [Authorize(Users = "")] would result in "no one" can access the action.

So Answer 1 and 2 is correct.

Source code of the AuthorizeAttribute

protected virtual bool AuthorizeCore(HttpContextBase httpContext) {
    if (httpContext == null) {
        throw new ArgumentNullException("httpContext");
    }

    IPrincipal user = httpContext.User;
    if (!user.Identity.IsAuthenticated) {
        return false;
    }

    if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) {
        return false;
    }

    if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) {
        return false;
    }

    return true;
}

And the Role and Users propertie.

public string Roles {
    get {
        return _roles ?? String.Empty;
    }
    set {
        _roles = value;
        _rolesSplit = SplitString(value); // simple split by comma
    }
}

public string Users {
    get {
        return _users ?? String.Empty;
    }
    set {
        _users = value;
        _usersSplit = SplitString(value); // simple split by comma
    }
}
葮薆情 2024-12-28 20:27:16

编辑:我已经对此进行了更改,因此它现在是正确的,只是补充了 dknaack 的正确答案

[Authorize] 装饰操作意味着用户必须经过身份验证。

因此,如果您希望任何登录用户都能够访问某个操作,通常只需输入 [Authorize]

dknaack 参考了源代码,所以他的答案一定是正确的,尽管对我来说似乎有点奇怪。但显然是对的!

只是补充一下,如果 _usersSplit 就像逗号上的正常分割一样,那么当 _users 时,我们期望 _usersSplit.Length1 ="" 我仍然是对的,但我猜 split 函数正在使用 RemoveEmptyEntries 选项。无法参考来源,因为现在我已经离开了(昨天进行了膝盖手术,目前还不允许在计算机上使用 - 哈哈)。

空字符串不是用户或角色的有效名称。请参阅此处:http://msdn.microsoft.com /en-us/library/8fw7xh74(v=VS.100).aspx

如果指定的用户中有任何一个,则应抛出 ArgumentException
名称或角色名称是空字符串,如果是,则抛出 ArgumentNullException
任何指定的用户名或角色名称为空(
Visual Basic)。

Edit: I have changed this so it's now correct and just supplements dknaack's correct answer

Decorating an action with [Authorize] means that the user must be authenticated.

So if you want any logged-in user to be able to access an action it's normal just to put [Authorize].

dknaack has referred to the source code, so his answer must be correct, even though it seems slightly strange to me. But clearly right!

Just to add, if _usersSplit is like a normal split on comma then we'd expect _usersSplit.Length to be 1 when _users ="" and I'd still be right, but I guess the split function is using the RemoveEmptyEntries option. Can't refer to the source for that as am afk right now (had knee operation yesterday, not allowed on computer just yet - lol).

Empty string is not a valid name for a user or a role. See here: http://msdn.microsoft.com/en-us/library/8fw7xh74(v=VS.100).aspx

You should throw an ArgumentException if any of the specified user
names or role names is an empty string and an ArgumentNullException if
any of the specified user names or role names is null (Nothing in
Visual Basic).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文