与 ASP.NET MVC 2 和区域的独立身份验证会话

发布于 2024-12-02 13:20:14 字数 479 浏览 4 评论 0原文

我正在开发一个小型 CMS 网站(前端区域),用户可以在其中查看新闻、探索产品等 在同一个站点中,我有一个后端区域,用于管理 CMS 的内容和 ERP 功能,这仅供公司员工使用 因此,我的网站分为两个区域,http://WEBSITEURL/Frontend/http://WEBSITEURL/Backend/

在前端区域中,我的用户根据客户存储库进行身份验证,但在我的后端区域,我的用户根据客户存储库进行身份验证公司的员工资源库。我想我必须编写自己的 MembershipProvider

我会在我的控制器中使用 Authorize 属性,但我想知道是否可以拥有并保留会话 2 身份验证

我想要一个 mvc 项目,而不是 2 个,一个用于前端和另一个用于后端

i am developing a little CMS website (frontend area) where users can see news, explore products, etc
and in the same site i have a backend area where CMS's content and ERP features are managed, this is just for company's staff
so, my website is splitted in 2 areas, http://WEBSITEURL/Frontend/ and http://WEBSITEURL/Backend/

in the frontend Area my users are authenticated against a customer repository, but in my backend area, my users are authenticated against a company's staff repository. i think i have to write my own MembershipProvider

i would use Authorize attribute in my controllers, but i want to know if it is possible to have and keep in session 2 authentications

i want to have ONE mvc project, not 2, one for frontend and the other for backend

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

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

发布评论

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

评论(1

岁吢 2024-12-09 13:20:14

不,您不需要重新发明 MembershipProvider。您也不需要重新发明表单身份验证。确实,你不应该。即使对于专家来说,正确实施安全措施也非常困难。您的新提供商是否容易受到填充预言机攻击,就像内置的提供商是

让我们一步一步地进行。

首先,为了为每个区域拥有单独的身份验证票证 (cookie),您可以使用 SetAuthCookie 的重载,允许您指定 cookie 路径。 设置每个 cookie 的路径,以便浏览器仅根据 URI 路径根为每个区域发送正确的 cookie (Frontend/Backend/,根据您的情况)。

AuthorizeAttribute 仍将按原样工作。如果执行上述步骤,浏览器将仅发送正确的 cookie,该 cookie 由表单身份验证进行签名和验证。 AuthorizeAttribute 只是检查当前提供者是否已完成此步骤,而不关心其实现。

重要我正在做出一个尚未实际测试的假设。我假设表单身份验证根据请求路径检查签名的 cookie,以确保路径相同。如果表单尚未执行此操作,您将需要自己测试并实现它。就像我说的,我认为是这样,但要测试一下才能确定。

关于“针对存储库进行‘身份验证’”,不要混淆身份验证和授权。身份验证意味着“你是谁?”让 forms auth 来做这件事。授权的意思是“你被允许做什么?”这是您检查存储库的地方。

所以最后,您将在员工/后端区域执行类似的操作:

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                if (StaffRepository.AuthorizedUser(model.UserName))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe, pathForBackend);
                    return RedirectTo....
                }
                else
                {
                    return MyUnauthorizedAction();
                }
            }

No, you don't need to reinvent MembershipProvider. You also don't need to reinvent forms authentication. Indeed, you shouldn't. Doing security correctly is incredibly hard, even for experts. Will your new provider be vulnerable to a padding oracle attack, like the built-in providers were?

Let's take this one step at a time.

First, in order to have separate authentication tickets (cookies) for each area, you use the overload of SetAuthCookie which allows you to specify a cookie path. Set the path for each cookie such that the browser only sends the correct cookie for each area based on the URI path root (Frontend/ or Backend/, in your case).

AuthorizeAttribute will still work as-is. The browser, if you do the step above, will only send the correct cookie, which is signed and validated by forms authentication. AuthorizeAttribute just checks to see that the current provider has done this step, without concerning itself with the implementation.

Important I'm making a presumption which I haven't actually tested. I'm presuming forms auth checks the signed cookie against the request path to ensure that the path is the same. You'll want to test this yourself and implement it if forms isn't already doing this. Like I said, I presume that it is, but test to be sure.

Regarding "'authenticating' against a repository," don't confuse authentication and authorization. Authentication means "who are you?" Let forms auth do that. Authorization means "what are you allowed to do?" That is where you check your repositories.

So in the end, you'll do something like this, in the staff/backend area:

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                if (StaffRepository.AuthorizedUser(model.UserName))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe, pathForBackend);
                    return RedirectTo....
                }
                else
                {
                    return MyUnauthorizedAction();
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文