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

mls*_*ves 11 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>
Run Code Online (Sandbox Code Playgroud)

在帐户的控制器中:

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();
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

Mik*_*ott 4

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