【问题标题】:Not all query parameters appearing in return url并非所有查询参数都出现在返回 url
【发布时间】:2020-03-05 10:42:33
【问题描述】:

我已经构建了一个 ASP.NET CORE MVC 应用程序并使用 cookie 身份验证。下面是我在 Startup.cs 文件中的代码。

services.AddAuthentication(options =>
{
    // these must be set other ASP.NET Core will throw exception that no
    // default authentication scheme or default challenge scheme is set.
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
  .AddCookie(options =>
   {
       options.LoginPath = "/Account/Login/";
   });

当 cookie 过期时,应用程序会重定向到 /Account/Login 路径,返回 URL 包含用户所在的当前 url。这可以正常工作,直到当前 url 具有 01 查询参数。如果当前 url 有 2 个查询参数,那么只有第一个查询参数被传递给返回 URL。登录方式如下。

public IActionResult Login(string returnUrl)
{
   return View(new LogInViewModel { ReturnUrl = returnUrl });
}

例如 如果当前 URL 是 /Inspection?inspectionID=1&processTypeID=2,则返回 url 仅获得 /Inspection?inspectionID=1processtypeID 参数不来。

但是当cookie过期时浏览器导航到/Account/Login的URL时,会显示正确的url/Account/Login?ReturnUrl=/Inspection?inspectionID=1&processTypeID=2

谁能指出为什么会发生这种情况以及如何解决这个问题?

谢谢, 泽涵

【问题讨论】:

  • 如果“ReturnUrl”是查询字符串,那么“processTypeID”是什么样的?

标签: asp.net-core-mvc cookie-authentication


【解决方案1】:

对重定向 uri 的值进行编码为我解决了这个问题。

在我的情况下,我有一个 blazor 页面:

<a href="auth/Signin?redirectUri=@RedirectUri">Sign In</a>

其中RedirectUri 返回完整的当前页面路径,包括多个查询参数。

我把链接改成:

<a href="auth/Signin?redirectUri=@UrlEncode(RedirectUri)">Sign In</a>,关键是UrlEncode(RedirectUri),解决了我的问题。

UrlEncode 可以在命名空间System.Web.HttpUtility 中找到。

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多