asp.net-mvc - 使用 ASP.NET MVC 和表单例份验证的不同 URL 的不同登录 URL

标签 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>

在帐户的 Controller 中:
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();
}

有一个更好的方法吗?

最佳答案

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

关于asp.net-mvc - 使用 ASP.NET MVC 和表单例份验证的不同 URL 的不同登录 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1327883/

相关文章:

asp.net - 防止 ASP.Net MVC 中的 Cookie 重放攻击

c# - ASP.NET MVC 3 中注销时清理浏览器缓存和 cookie 的问题

c# - 默认登录 asp.net MVC3 重定向到错误的 View 。这套在哪里?

c# - 无法将 lambda 表达式转换为类型 'CookieAuthenticationOptions',因为它不是委托(delegate)类型

c# - PCL 库可以与 ASP.NET MVC 一起运行吗?

c# - 为什么我不能让 LINQ 在我的 .cshtml 文件中工作?

asp.net-mvc - WsFederation 和本地用户混合认证

asp.net - Web配置错误:无法识别的属性'xmlns:xdt'。请注意,属性名称区分大小写

c# - 我怎样才能更好地处理 ASP.NET MVC 中有关我的部分 View 的这种情况?

当 plist 文件受表单例份验证保护时,iOS 无线安装失败