如何重定向到returnUrl在Asp.Net MVC5中工作

Ola*_*son 10 asp.net asp.net-mvc redirect login asp.net-mvc-5

我已经开始使用新的Asp.Net Identity with Owin创建了一个新的MVC 5站点.在我的"帐户"控制器中,它具有[授权]属性,我有相当标准的操作;

   // GET: /User/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        } 

// POST: /User/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var userApi = new UserService();
                    var apiUser = await userApi.LogIn(UserManager, model.CardNumber, model.Pin, model.RememberMe);

                    if (apiUser != null)
                    {
                        await SignInAsync(apiUser, model.RememberMe);
                        if (string.IsNullOrEmpty(returnUrl))
                        {                                   
                            return RedirectToAction("UserLoggedIn", "User");    
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid username or password.");
                    }
                }

            }
            catch (Exception ex)
            {
                Trace.TraceError("Cannot login {0}", ex.ToString());
                Response.AppendToLog(ex.ToString());
                ModelState.AddModelError("", ex.ToString());
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是关于returnUrl行为,上面的代码工作的意义是,如果用户没有登录并在具有属性[Authorize]的控制器中调用一个动作,它将被发送到上面的登录操作,然后返回到请求的控制器/操作.哪个好,但是怎么样?它安全吗?

在这篇关于" 防止开放式重定向攻击 "的文章中(对于早期版本的Asp.Net MVC),建议在进行重定向之前检查一下returnUrl它是一个本地URL,是我应该做的还是它呢?现在由框架处理?

干杯,奥拉

Mar*_*ijn 15

您需要使用此方法检查URL是否确实是本地的(它不​​是由框架自动处理的):http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.islocalurl% 28V = vs.118%29.aspx

if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
  return Redirect(returnUrl);
}
Run Code Online (Sandbox Code Playgroud)

  • 我找到了这个谈论它http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx但是并没有真正变得更加明智我必须承认. (2认同)

Kir*_*ten 5

正如 Sandeep Phadke 所说,由于 startup.Auth.cs 中的配置,returnUrl 参数已被填充。

CookieAuthenticationOptions 有一个属性 ReturnUrlParameter,默认设置为“returnUrl”。这就是为什么它看起来像魔术的原因。您可以将其更改为您想要的任何内容:

app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        ReturnUrlParameter = "returnTo"
    });
Run Code Online (Sandbox Code Playgroud)

然后您可以将 AccountController Login-Action 更改为:

[AllowAnonymous]
    public ActionResult Login(string returnTo)
    {
        ViewBag.ReturnUrl = returnTo;
        return View();
    } 
Run Code Online (Sandbox Code Playgroud)