Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 不同URL的不同LoginUrl';使用ASP.NET MVC和窗体身份验证_Asp.net Mvc_Forms Authentication - Fatal编程技术网

Asp.net mvc 不同URL的不同LoginUrl';使用ASP.NET MVC和窗体身份验证

Asp.net mvc 不同URL的不同LoginUrl';使用ASP.NET MVC和窗体身份验证,asp.net-mvc,forms-authentication,Asp.net Mvc,Forms Authentication,我有一个ASP.NETMVC项目,我想为网站的不同区域提供不同的LoginUrl。根据站点的区域,输入不同类型的凭据 http://host.com/widget/home应将用户重定向到http://host.com/widget/logon http://host.com/admin/home应将用户重定向到http://host.com/admin/logon 到目前为止,我提出的最佳解决方案是在web.config中使用表单Auth loginUrl=“~/Account/Logon”:

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

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

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

到目前为止,我提出的最佳解决方案是在web.config中使用表单Auth loginUrl=“~/Account/Logon”:

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

有更好的方法吗?

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


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

将身份验证属性添加到操作中


然后在global.asax add Application\u AuthenticateRequest中,查看发件人并将其重定向到您希望操作登录的位置。

我在stackoverflow问题中询问并回答了这个问题。

我只是好奇,但这似乎与我最初做的类似,只是在不同的位置。这样做的好处是什么?在MVC区域中,确实有单独的web.config,但如果尝试添加身份验证属性,则可能会出现错误:在应用程序级别之外使用注册为allowDefinition='MachineToApplication'的节是错误的。此错误可能是由于未将虚拟目录配置为IIS中的应用程序所致。顺便说一句:您可以将returnUrl作为字符串参数添加到方法:LogOn(string returnUrl)
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();
}