如何在 MVC 2 中进行授权?
如何在 MVC 2 中进行授权?
我想使用 AD 组/角色而不是提供的默认值。这似乎是“AspNetSqlMembershipProvider”。
无论如何,我输入:
[Authorize(Users = "username")]
public ActionResult About()
{
ViewData["Welcome"] = "Welcome About";
return View();
}
然后加载页面给我:连接名称“ApplicationServices”未找到 应用程序配置或连接字符串为空。
Line 34: <providers>
Line 35: <clear />
Line 36: <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
Line 37: </providers>
Line 38: </membership>
我读了 此 stackoverflow,但在创建自定义类之后< code>AuthorizationAttribute 扩展了 ActionFilterAttribute
ContextCache、IoC 和许多其他问题无法解决,并且不确定从哪里开始。我还阅读了this stackoverflow,它建议以不同的方式处理它,开始得到使困惑。
如何在 MVC 应用程序中使用 AD 组而不是 AspNetSqlMembershipProvider
?
额外问题:假设我的页面有一个“编辑”按钮。我可以添加逻辑来决定是否根据授权呈现此按钮吗?
感谢您的帮助。
编辑:一些进一步的信息。
我无意阻止或允许所有人访问此网站。
我打算有 3 个基本用户组来区分访问级别,即超级管理员、管理员、 基本访问。
不会有登录表单,当用户访问网站时,我们将检查用户是哪个组的成员,然后根据该结果呈现页面。
例如,“基本访问”组中的用户“bob”将点击页面,并且“编辑”、“删除”等按钮/操作被禁用,因此基本上是一个只读组。但是“超级管理员”组中的用户“jim”拥有可用的所有操作/按钮。我怎样才能做到这一点?
How do I go about Authorization in MVC 2?
I want to use AD groups/roles rather than the default that is provided. That seems to be "AspNetSqlMembershipProvider".
Anyway I put :
[Authorize(Users = "username")]
public ActionResult About()
{
ViewData["Welcome"] = "Welcome About";
return View();
}
And then loading the page gives me: The connection name 'ApplicationServices' was not found in
the applications configuration or the connection string is empty.
Line 34: <providers>
Line 35: <clear />
Line 36: <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
Line 37: </providers>
Line 38: </membership>
I read this stackoverflow, but after creating a custom class AuthorizationAttribute
extending ActionFilterAttribute
ContextCache, IoC and a number of other things could not resolve, and not really sure where to go from there. I also read this stackoverflow and it suggests going about it differently, starting to get confused.
How do I go about using AD groups rather than AspNetSqlMembershipProvider
in MVC app ?
Bonus question: Say I have a "Edit" button a page. Can I add logic to decide whether to render this button based on the Authorization ?
Thank you for your help.
Edit: some further information.
I do not intend to block or allow ALL access to this site.
I intend to have 3 basic user groups differentiating level of access, i.e. Super Admin, Admin,
Basic Access.
There will be no log in form, when the user hits the site we will check which group the user is a member of- then the page renders based on that.
So for example, user 'bob' in 'Basic Access' group will hit the page and buttons/actions like "Edit", "Delete" are disabled, so basically a read only group. But user 'jim' in group 'Super Admin', has all actions/buttons available to him. How could I achieve this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看 Windows 身份验证
仍然使用控制器/操作上的
Authorize
属性,但将您的站点配置为使用 Windows 身份验证。额外答案:要检查代码中的身份验证和授权,您可以从控制器使用以下其中一项:
You should look into Windows Authentication
Still use the
Authorize
attribute on your controllers/actions, but configure your site to use Windows Authentication instead.Bonus answer: To check authentication and authorization in code, you can use one of the following from a controller:
使用 Windows 身份验证的答案非常有效,但有以下注意事项。
首先,服务器必须加入您的域。如果有防火墙,它必须具有免费的 AD 访问权限。
其次,您必须接受弹出登录对话框,而不是使用基于表单的登录。
如果您需要使用表单登录的 AD,那么需要做更多的工作。您能更具体地说明您的需求吗?
The answers to use Windows authentication work great, with the following caveats.
First, the server must be joined to your Domain. And it has to have free AD access if there are any firewalls in place.
Second, you have to be ok with having a popup dialog for login, rather than using a form based login.
If you need AD with forms login, then there's more work involved. Can you be more specific about your needs?
好吧,您可以通过 webconfig 限制对该站点的访问。
这将阻止给定广告组中未列出的任何其他广告组。
在 IIS 中,您需要禁用匿名访问并启用 Windows 身份验证
well, you can restrict access to the site via webconfig.
this will block any others not listed in the given ad groups.
in IIS you will need to disable anon access and enable windows auth