如何掩饰' ReturnUrl'在网址?

时间:2016-10-17 11:46:20

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

我有一个asp.net mvc项目,当我访问loginPage时,我的网址如下:

  

http://localhost:63356/Account/Login?ReturnUrl=%2F

我不希望它看起来像这样,在routConfig文件中我做到了这一点:

routes.MapRoute(
   name: "LogIn",
   url: "LogIn",
   defaults: new { controller = "Account", action = "Login" }
);

哪个网址应该是这样的:

  

http://localhost:63356/Login

我错过了什么?

编辑:

登录操作:

[AllowAnonymous]
public ActionResult Login()
{
  return View();
}

编辑2: @pwas提到这是在视图中设置的,所以我通过删除returnUrl参数来改变它:

<section role="main" id="login">
    <div class="panel center-block logInBlock" style="width:300px;">
        <div class="panel-heading loginHeadPadding"><h1>Logga in</h1></div>
        <div class="panel-body loginBodyPadding">

            @using (Html.BeginForm("Login", "Account", new {  }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <div class="form-group text-left">
                    @Html.LabelFor(m => m.UserName, "Användarnamn")
                    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", placeholder = "Användarnamn" })
                </div>
                <div class="form-group text-left">
                    @Html.LabelFor(m => m.Password, "Lösenord")
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Lösenord" })
                </div>

                <input type="submit" value="Logga in" class="btn btn-primary btn-block" />

                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            }
        </div>
    </div>
</section>

问题仍然存在

1 个答案:

答案 0 :(得分:1)

在文件Startup.Auth.cs中,MVC模板可能生成了这样的块:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    // ...
    LoginPath = new PathString("/Account/Login"),
    // ... more ...
});

将其更改为:

    LoginPath = new PathString("/Login"),

然后它将起作用,并与您已经定义的路线相结合。


摆脱ReturnUrl=是另一回事,它是由ASP.NET授权机制强有力地实现的 您可以将名称更改为例如from=将其放在上面的块中:

    ReturnUrlParameter = "from",

将其设置为""不会删除它,只是使用的名称将为空。此外,您还必须在其他几个地方进行更改,以确保新名称随处可用。

要完全删除它有各种指南,但我相信大多数(如果不是全部)都涉及重定向或URL重写。

相关问题