【问题标题】:How can I change the name of the "ReturnUrl" parameter used by ASP.NET MVC?如何更改 ASP.NET MVC 使用的“ReturnUrl”参数的名称?
【发布时间】:2015-12-11 18:48:27
【问题描述】:

ReturnUrl 有点丑。我想改用redirect。如何指定应该用于表单身份验证重定向 URL 的参数名称以及 [Authorize] 属性?还是我必须创建一个 IAuthorizationFilter 实现? :(

例子:

[Authorize]
public class Tools : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

当没有登录的用户访问http://example.com/tools时,我希望他们被重定向到http://example.com/account/logon?redirect=%2ftools,而不是默认的http://example.com/Account/LogOn?ReturnUrl=%2ftools

对于 /account/logon 部分,我可以在 Global.asax 中修改我的路由并进行更改

<authentication mode="Forms">
  <forms loginUrl="~/account/logon" timeout="2880" />
</authentication>

在 web.config 中。但是我不知道如何更改 ReturnUrl 参数。

【问题讨论】:

标签: c# asp.net-mvc-3


【解决方案1】:

将此密钥添加到 web.config 的 appSettings 部分

<add key="aspnet:FormsAuthReturnUrlVar" value="redirect" />

【讨论】:

  • 我认为这应该被标记为答案,而不是下面的另一个……你好 2018 :)
【解决方案2】:

这里的问题和答案似乎与旧的表单身份验证有关。在较新版本的 MVC 上,例如MVC 5(使用 Identity 2.0),你会在 Startup.Auth.cs 中做这样的事情:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/account/login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            ReturnUrlParameter = "redirect"
        });

重要的部分当然是ReturnUrlParameter = "redirect"(可以是任何东西)。其余的可能因您的项目而异。

【讨论】:

    【解决方案3】:

    不是最好的解决方案,但它可以工作......

    <rule name="FormsAuthentication" stopProcessing="true">
      <match url="^account/log(i|o)n$" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="^ReturnUrl=([^=&amp;]+)$" />
      </conditions>
      <action type="Redirect" url="account/logon?redirect={C:1}" appendQueryString="false" />
    </rule>
    

    【讨论】:

      【解决方案4】:

      这里的问题是重定向不是帖子。这是一个得到。在 get 上传递变量的唯一方法是使用某种类型的查询字符串参数。您可以伪装此 url 重写,但它仍然是一个查询参数,并在 URL 上传递。

      也许您可以更清楚地了解您在寻找什么?

      【讨论】:

      • 我不确定您是否理解我的问题...我添加了一个示例,希望这将使我的要求更清楚。
      • 是的,我明白你现在的意思了.. 这似乎发生在链条的上游。当返回 401 时,它会被捕获并发出重定向。我正在调查。
      【解决方案5】:

      只需在键值对之后的 appSettings 部分中添加您的 web.config:

      <add key="aspnet:FormsAuthReturnUrlVar" value="your-custom-parameter-name"/>
      

      【讨论】:

        【解决方案6】:

        无法使用配置更改参数名称,因为“ReturnUrl”参数名称是硬编码在 System.Web.Security.FormsAuthentication 类中的,该类是用于表单身份验证的类,包括重定向。

        实现所需结果的一种方法是扩展 Authorize 属性,使其重定向到带有您自定义参数名称的登录页面。然后根据您使用的 FormsAuthentication 的其他方法,您也可以修改这些方法,特别是 FormsAuthentication.RedirectFromLoginPage。

        【讨论】:

          【解决方案7】:

          参数名不能改,烦人。我通过编写自己的身份验证模块解决了这个问题——你需要知道身份验证在内部是如何工作的,但这并不难——看看它是如何在反射器中完成的(可能会简化它,我最终只使用了来自 FormsAuthentication 的 cookie 加密/解密)。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-25
          • 1970-01-01
          • 2011-01-03
          相关资源
          最近更新 更多