AuthorizeAttribute 构造函数参数 Roles equals string.empty 含义?
这是一些 MCTS 70-515 考试练习测试中的问题。
请帮助提供正确的 2 个答案
您正在实现一个 ASP.NET MVC 2 Web 应用程序,该应用程序允许用户查看和编辑数据。您需要确保只有登录用户才能访问控制器的编辑操作。您可以添加到“编辑”操作以实现此目标的两个可能属性是什么?
(每个正确答案都提供一个完整的解决方案。选择两个。)
- [Authorize(Users = "")]
- [Authorize(Roles = "")]
- [Authorize(Users = "*")]
- [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.)
- [Authorize(Users = "")]
- [Authorize(Roles = "")]
- [Authorize(Users = "*")]
- [Authorize(Roles = "*")]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看AuthorizeAttribute的源代码发现没有通配符“*”。
如果
[Authorize(Users = "")]
会导致“没有人”可以访问该操作,则毫无意义。所以答案1和2是正确的。
AuthorizeAttribute 的源代码
以及
Role
和Users
属性。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
And the
Role
andUsers
propertie.编辑:我已经对此进行了更改,因此它现在是正确的,只是补充了 dknaack 的正确答案
用
[Authorize]
装饰操作意味着用户必须经过身份验证。因此,如果您希望任何登录用户都能够访问某个操作,通常只需输入
[Authorize]
。dknaack 参考了源代码,所以他的答案一定是正确的,尽管对我来说似乎有点奇怪。但显然是对的!
只是补充一下,如果
_usersSplit
就像逗号上的正常分割一样,那么当_users 时,我们期望
我仍然是对的,但我猜 split 函数正在使用_usersSplit.Length
为1
=""RemoveEmptyEntries
选项。无法参考来源,因为现在我已经离开了(昨天进行了膝盖手术,目前还不允许在计算机上使用 - 哈哈)。空字符串不是用户或角色的有效名称。请参阅此处:http://msdn.microsoft.com /en-us/library/8fw7xh74(v=VS.100).aspx
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 be1
when_users =""
and I'd still be right, but I guess the split function is using theRemoveEmptyEntries
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