ASP.NET MVC - 重定向到正确的成员资格提供程序

发布于 2024-12-01 18:05:39 字数 623 浏览 1 评论 0原文

我有一个具有买家和卖家功能的 ASP.NET MVC 3 应用程序。买方和卖方的“安全”数据库是分开的,并且每个数据库都有不同的会员提供商(具有不同的要求)。 MVC 应用程序分为多个区域,一个用于买家,一个用于卖家(添加了一些常见的内容)。现在一切正常,因为我为买家和卖家提供了不同的登录屏幕。要求发生了变化,我需要为买家和卖家使用相同的登录屏幕(和重置密码屏幕)。

理想情况下,我希望流程如下:

  • 买家或卖家输入他们的电子邮件地址和密码
  • 提交表单后,一个名为“操作”的常见控制器操作会
  • 调用逻辑来查看买家表。如果此电子邮件地址存在,则重定向到买家帐户控制器中的登录操作。
  • 否则,如果卖家表中存在电子邮件地址,则重定向到卖家帐户控制器中的登录操作

理论上,这应该可以正常工作。但是,我知道不可能使用 POST 请求重定向到另一个操作。

我想知道的是:

  1. 是否有任何理由不将我的买家/卖家登录操作更改为使用 GET 而不是 POST,因为它们不会在我的代码中的其他任何地方被调用?这看起来像是一个黑客,但我不知道为什么......
  2. 你能想出一个更好的方法来实现我想要实现的目标吗?

预先感谢,

JP

I have an ASP.NET MVC 3 application with buyer and seller functionalities. The buyer and seller 'security' databases are separated and there are different membership providers (with different requirements) for each. The MVC application is broken into areas, with one for buyers and one for sellers (with the addition of some common stuff). Everything is working fine right now as I have different login screens for buyers and sellers. A requirement has changed whereby I need to use the same login screen (and reset password screen) for both buyers and sellers.

Ideally I would like the process to flow as follows:

  • Buyer or seller enters their email address and password
  • Upon form submission a common controller action is called
  • Action calls logic to look in buyer table. If this email address is present, redirect to logon action in buyer account controller
  • Else, if email address is present in seller table, redirect to logon action in seller account controller

In theory this should work fine. However, I am aware that it is not possible to redirect to another action using a POST request.

What I would like to know is:

  1. Is there any reason not to change my buyer/seller logon actions to use GET rather than POST given that they will not be called anywhere else in my code? It seems like a hack, but I don't know why...
  2. Can you think of a better way to achieve what I am trying to achieve?

Thanks in advance,

JP

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

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

发布评论

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

评论(1

妖妓 2024-12-08 18:05:39

您应该有一个 LoginController。它只能有一个 Authorize(string email, string password) 操作。进行“拆分”的方法是创建一个 MembershipProviderFactory (或任何您想命名的名称),它将 IMembershipProvider 接口返回到您的控制器。工厂可以通过查询数据库返回正确类型的 MembershipProvider

public class SellerMembershipProvider : IMembershipProvider
{
  public bool Authorize(string email, string password)
  {
    // validate seller
  }
}

public class BuyerMembershipProvider : IMembershipProvider
{
  public bool Authorize(string email, string, password)
  {
    // validate buyer
  }
}

public class MembershipProviderFactory
{
  public IMembershipProvider Create(string email)
  {
     if(/* email is in seller database*/)
     {
       return new SellerMembershipProvider();
     }
     else
     {
       return new BuyerMembershipProvider();
     }
  }
}

public class LoginController : Controller
{

  private readonly MembershipProviderFactory _providerFactory = new MembershipProviderFactory();
  public ActionResult Authenticate(string email, string password)
  {

    IMembershipProvider provider = _providerFactory.Create(email);
    if(provider.Authorize(email, password))
    {
    return View("MyAccount");
    }
    else
    {
        return View("Login");
    }
  }
}

You should have a single LoginController. It can have just one Authorize(string email, string password) action. Where you do "the split" is by creating a MembershipProviderFactory (or whatever you want to name it) that returns an IMembershipProvider interface to your controller. The factory can return the correct type of MembershipProvider by querying the databases.

public class SellerMembershipProvider : IMembershipProvider
{
  public bool Authorize(string email, string password)
  {
    // validate seller
  }
}

public class BuyerMembershipProvider : IMembershipProvider
{
  public bool Authorize(string email, string, password)
  {
    // validate buyer
  }
}

public class MembershipProviderFactory
{
  public IMembershipProvider Create(string email)
  {
     if(/* email is in seller database*/)
     {
       return new SellerMembershipProvider();
     }
     else
     {
       return new BuyerMembershipProvider();
     }
  }
}

public class LoginController : Controller
{

  private readonly MembershipProviderFactory _providerFactory = new MembershipProviderFactory();
  public ActionResult Authenticate(string email, string password)
  {

    IMembershipProvider provider = _providerFactory.Create(email);
    if(provider.Authorize(email, password))
    {
    return View("MyAccount");
    }
    else
    {
        return View("Login");
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文