如何在 MVC 2 中进行授权?

发布于 2024-11-28 08:31:08 字数 1790 浏览 1 评论 0原文

如何在 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 技术交流群。

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

发布评论

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

评论(3

负佳期 2024-12-05 08:31:08

您应该查看 Windows 身份验证

仍然使用控制器/操作上的 Authorize 属性,但将您的站点配置为使用 Windows 身份验证。

额外答案:要检查代码中的身份验证和授权,您可以从控制器使用以下其中一项:

this.User.Identity.IsAuthenticated
this.User.Identity.Name
this.User.IsInRole("roleName")

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:

this.User.Identity.IsAuthenticated
this.User.Identity.Name
this.User.IsInRole("roleName")
神回复 2024-12-05 08:31:08

使用 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?

微暖i 2024-12-05 08:31:08

好吧,您可以通过 webconfig 限制对该站点的访问。

    <authentication mode="Windows" />
    <authorization>
        <allow roles="[YOURADSERVER]\[YOUR AD GROUP]"/>
        <deny users="*"/>
    </authorization>

这将阻止给定广告组中未列出的任何其他广告组。

在 IIS 中,您需要禁用匿名访问并启用 Windows 身份验证

well, you can restrict access to the site via webconfig.

    <authentication mode="Windows" />
    <authorization>
        <allow roles="[YOURADSERVER]\[YOUR AD GROUP]"/>
        <deny users="*"/>
    </authorization>

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

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