代码之家  ›  专栏  ›  技术社区  ›  mlsteeves

使用ASP.NET MVC和表单身份验证为不同的URL提供不同的登录URL

  •  11
  • mlsteeves  · 技术社区  · 16 年前

    我有一个ASP.NET MVC项目,我想为网站的不同区域提供不同的登录URL。根据站点的区域,输入不同类型的凭据。

    http://host.com/widget/home 应将用户重定向到 http://host.com/widget/logon .

    http://host.com/admin/home 应将用户重定向到 http://host.com/admin/logon .

    到目前为止,我提出的最佳解决方案是在web.config中使用forms auth loginurl=“~/account/logon”:

       <authentication mode="Forms">
          <forms loginUrl="~/Account/LogOn" timeout="2880"/>
       </authentication>
    

    在帐户的控制器中:

    public ActionResult LogOn()
    {
       //redirect depending on the returnUrl?
       string returnUrl = ControllerContext.Controller.ValueProvider["ReturnUrl"].AttemptedValue;
       if (returnUrl.StartsWith("/widget"))
       {
           return Redirect(string.Format("/widget/Logon?ReturnUrl={0}", returnUrl));
       }
       if (returnUrl.StartsWith("/admin"))
       {
           return Redirect(string.Format("/admin/Logon?ReturnUrl={0}", returnUrl));
       }
       return View();
    }
    

    有更好的方法吗?

    3 回复  |  直到 16 年前
        1
  •  4
  •   Community Mohan Dere    9 年前

    我在StackOverflow问题中提出并回答了这个问题 How to redirect to a dynamic login URL in ASP.NET MVC .

        2
  •  1
  •   Deeksy    16 年前

    我知道在网站的子文件夹中可以有单独的web.config文件,这样,如果在管理/文件夹中有实际的.aspx页面,并且在该文件夹中有web.config,则可以单独指定该文件夹中的身份验证URL。

    我不确定这是否适用于ASP.NET MVC路由,因为这些子文件夹中可能没有物理文件,但值得一试。

        3
  •  1
  •   user162715    16 年前

    向操作添加authenticate属性。

    然后在global.asax中添加应用程序_authenticateRequest,然后查看发送者并将其重定向到您希望操作登录的位置。