使用ASP.NET MVC和表单身份验证的不同URL的不同LoginUrl

时间:2009-08-25 12:11:34

标签: asp.net-mvc forms-authentication

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

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 个答案:

答案 0 :(得分:4)

我在stackoverflow问题How to redirect to a dynamic login URL in ASP.NET MVC中询问并回答了这个问题。

答案 1 :(得分:1)

我知道你可以在网站的子文件夹中有单独的web.config文件,这样如果你在admin /文件夹中有实际的.aspx页面,并且在该文件夹中有web.config,你可以指定分别在该文件夹中验证网址。

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

答案 2 :(得分:1)

为您的操作添加Authenticate属性。

然后在global.asax中添加Application_AuthenticateRequest,然后查看发件人并将其重定向到您希望操作登录的位置。