将 2 个不同的用户数据库集成到单个 ASP.NET MVC 成员提供程序中?

发布于 2024-08-02 09:51:20 字数 494 浏览 3 评论 0原文

我正在开发一个项目,需要根据两个不同数据库中的记录对用户进行身份验证。所有管理员都存储在本地,并且需要完整的功能来管理其帐户(重置密码等)。普通用户根据另一个 Web 应用程序使用的不同数据库进行身份验证,因此我只需要检查他们的凭据是否正确。

在登录屏幕输入用户名/密码后,我的应用程序应该检查它们是否存在于本地管理员表中。如果是这样,他们将被授予“管理员”角色并被允许访问。如果没有,它应该检查其他应用程序的用户表,如果成功的话,给他们一个“用户”角色。

该项目基本上是一本大型在线书籍。用户只需进行身份验证即可查看它、对各个部分进行评分以及为页面添加书签。评级/书签数据将与其唯一的 ID 相关联。所有用户管理均在外部应用程序中处理。但是,管理员只能查看/编辑页面,而不能对内容进行评级/添加书签。他们的帐户将通过此管理区域进行管理。

在 .NET MVC 应用程序中实现此目的的最佳方法是什么?我所说的“这个”,是指将登录/身份验证系统与两者集成,并根据哪个数据库确认其凭据来为它们分配角色。

提前致谢!

I'm working on a project that needs to authenticate users based on records in two different databases. All administrators are stored locally and require full functionality to manage their accounts (reset password, etc). Regular users are authenticated against a different database used by another web app, so I only need to check that their credentials are correct.

After entering their username/pass at the logon screen, my app should check if they exist in the local admins table. If so, they are given the role of 'admin' and allowed access. If not, it should then check the other app's user table and give them a 'user' role if successful.

The project is basically a large online book. Users simply need authentication to view it, rate the sections, and bookmark pages. The rating/bookmark data will be associated with their unique id. All user management is handled in the external app. Admins, however, will only be able to view/edit the pages and will NOT be rating/bookmarking things. Their accounts will be managed with this admin area.

What is the best way to accomplish this in a .NET MVC application? By 'this', I mean integrating the logon/authentication system with both and assigning them a role based on which database confirms their credentials.

Thanks in advance!

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

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

发布评论

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

评论(1

一念一轮回 2024-08-09 09:51:20

MVC 实际上与您的用户验证逻辑没有太大关系 - 您需要 实现自定义成员资格提供程序来处理与两个数据库的连接并执行验证。不过,此成员资格类别可以移植到任何应用程序,但它并非特定于 MVC。

一旦您在自定义成员资格提供程序中获得了逻辑,您只需将 FormsAuthentication 与您的 MVC 应用程序一起使用,其中有很多教程,这是一个快速的

我要添加的与 MVC 相关的唯一提示是,您应该尝试在控制器中保留视图决策的逻辑。在视图中添加类似“<% if user == admin then renderPartial(this) else renderPartial(that) %>”的内容很诱人,但我认为这违反了 MVC 原则。最好在控制器中使用 ViewModel 或填充 ViewData。

MVC really doesn't have much to do with your user validation logic - you'll need to implement a custom membership provider to handle connecting to both databases and performing the validation. This membership class could be ported to any application though, it's not specific to MVC.

Once you've got your logic in your custom membership provider, you just need to use FormsAuthentication with your MVC app, of which there are lots of tutorials around, here's a quick one.

The only tip that I would add that pertains to MVC is that you should try to keep your logic for view decisions in your controllers. It's tempting to put something like "<% if user == admin then renderPartial(this) else renderPartial(that) %>" in your View, but that violates MVC principles in my opinion. Much better to use ViewModels or populate ViewData in your controller.

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