在 ASP.NET MVC 3 中实现访问控制

发布于 2024-11-26 04:10:21 字数 133 浏览 3 评论 0原文

我正在 ASP.NET MVC 3 中编写一个基于角色的应用程序。显然,根据用户所处的角色,将取决于他们有权访问哪些页面或功能。我不太确定在 MVC 应用程序中实现此功能的最佳位置或方法。任何信息或某些资源的指向将不胜感激。

提前致谢。

I'm writing a role based application in ASP.NET MVC 3. Obviously, depending on the role the user is in, will depend what pages or functions they have access to. I'm not really certain the best place, or way to implement this in an MVC application. Any information or points to certain resources would be appreciated.

Thanks in advance.

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

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

发布评论

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

评论(2

嘿看小鸭子会跑 2024-12-03 04:10:21

如果您想实现自己的会员系统,那么您可以按照以下步骤操作:

  1. 将 HttpModule(继承自 IHttpModule 的类)以安全请求(您想要为其定义访问控制的请求)的方式放置
  2. 在该模块中,创建一个处理程序AuthenticateRequest 事件
  3. 在该处理程序中,检查请求中的身份验证 Cookie。该 cookie 可以是您的,或者您可以使用 FormsAuthentication 类为您创建、加密和解密 cookie。
  4. 如果 cookie 存在,则加载用户的角色,并将它们作为键/值对存储在 HttpContext.Current.Items 中。这样,您就可以在任何您想要的地方使用它。
  5. 如果没有,则将用户重定向到登录页面。登录页面的地址可以从settings或web.config中获取。
  6. 在登录页面中,获取用户的登录信息,包括用户名和密码,然后创建一个Authentication Cookie并发送给客户端。

If you want to implement your own membership system, then you can follow these steps:

  1. Put an HttpModule (a class inheriting from IHttpModule) in the way of secure requests (requests you want to define access control for)
  2. In that module, create a handler AuthenticateRequest event
  3. In that handler, check the request for an Authentication Cookie. This cookie could be yours or you can use FormsAuthentication class to create, encrypt and decrypt cookies for you.
  4. If cookie is present, then load the roles of the user, and store them in HttpContext.Current.Items as a key/value pair. This way, you can use it anywhere you want.
  5. If not, then redirect the user to login page. The address of the login page could be retrieved from settings or web.config
  6. In login page, get the login information of user including user name and password, then create an Authentication Cookie and send it to the client.
万劫不复 2024-12-03 04:10:21

Membership Provider 内置于 .NET 中,可以从 MVC3 中使用。我链接到的帖子概述了如何使用 .NET 框架附带的默认实现,可以 创建您自己的,但我建议首先熟悉默认值。

The Membership Provider is built into .NET and can be used from MVC3. The post I linked to is an overview how to use the default implementation that ships with the .NET framework, it's possible to create your own but I suggest getting familiar with the default first.

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