【问题标题】:Working with return url in asp.net core在 asp.net 核心中使用返回 url
【发布时间】:2018-01-30 06:02:39
【问题描述】:

如果用户在访问特定 URL 时未经过身份验证/授权,我们会尝试将用户(使用返回 URL)重定向到登录页面。但是,在将用户重定向到登录页面时,我们无法在路由中添加自定义参数(在这种情况下为客户端名称)。我们正在使用 asp.net 身份核心框架。

Startup.cs 中,我们定义了以下适用于所有人的路由。

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "Edge",
                    template: "{clientname}/{controller}/{action}");
            });

还在代码行下方添加以确保所有 URL 都需要身份验证

services.AddMvc(o =>
{
   o.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
})

并配置IdentityOptions重定向到登录页面如下

services.Configure<IdentityOptions>(opt =>
{
  opt.Cookies.ApplicationCookie.LoginPath = new PathString("/Account/Login");
});

在下面的帐户控制器中是登录方法

[HttpGet]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
    this.ViewData["ReturnUrl"] = returnUrl;
    return View();
}

如果用户试图访问任何未经身份验证的 URL,它应该重定向到登录页面。下面以 Home Controller 中的 Index 方法为例。

public IActionResult Index()
{
    return View();
}

但是,每当我们尝试将用户重定向到登录页面时,它都不会在 URL 中附加客户端名称。它在/Account/Login 中缺少客户端名称的 URL 下方形成

http://localhost:5002/Account/Login?ReturnUrl=/ClientA/home/index

因此,导致 404 Page not found 错误。那么我们需要做些什么来正确重定向。

Url 格式如下

http://localhost:5002/ClientA/Account/Login?ReturnUrl=/ClientA/home/index

【问题讨论】:

  • 你能把整个 routeconfig.cs 贴出来
  • 我正在使用 asp.net 核心,其中 routeconfig.cs 不存在。我们在 Startup.cs 文件中定义了我在代码中提供的路由

标签: c# asp.net-mvc asp.net-core asp.net-core-mvc asp.net-identity


【解决方案1】:

似乎他们在 .Net Core MVC 中对其进行了更改

它对我有什么作用:

public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = "")
{
    ....... other codes

    if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
       return Redirect(returnUrl);
    else
       return RedirectToAction("Index", "Home");
}

现在转到 HTML Razor 代码:

@{
    ViewData["Title"] = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var returnUrl = @Context.Request.Query["returnurl"];
}

<form asp-action="Login" asp-route-returnurl="@returnUrl">
   <!--Rest of your login page HTML -->
</form>

现在运行顺利!

【讨论】:

    【解决方案2】:

    您在身份验证选项上专门设置了 LoginPath。默认情况下,无论您尝试访问的资源如何,它都会在您未经身份验证时将您定向到那里。我相信您可能必须替换或继承/覆盖一些内部结构,以便根据您请求的资源使 LoginPath 动态化。我不确定是否本机支持动态登录路径?我可能是错的。

    在不相关的安全说明中,您应该在尝试使用 ReturnUrl 中的资源之前验证它是否是您的应用程序的本地资源,甚至返回您的应用程序的主页。否则,格式错误的 URL 可能会将重定向位置欺骗到旨在模仿真实外观但具有恶意意图的资源。

    if (Url.IsLocalUrl(returnUrl))
        return Redirect(returnUrl);
    else
        return RedirectToAction("Index", "Home");
    

    【讨论】:

    • 是的,我在身份验证选项中专门设置了 LoginPath,因为我无法在登录路径中添加客户端名称或无法动态构建登录路径。所以这是我的问题应该怎么做?
    【解决方案3】:

    您可以使用Events 获取请求并重定向到您想要的网址,就像这个示例一样。

    services.ConfigureApplicationCookie(options => {
    
                options.Events = new Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationEvents
                {
                    OnRedirectToLogin = ctx =>
                    {
                        var requestPath = ctx.Request.Path;
                        if (requestPath.Value == "/Home/About")
                        {
                            ctx.Response.Redirect("/Home/UserLogin");
                        }
                        else if (requestPath.Value == "/Home/Contact")
                        {
                            ctx.Response.Redirect("/Home/AdminLogin");
                        }
    
                        return Task.CompletedTask;
                    }
                };
    
            });
    

    查看此链接:How to redirect access denied login based on the URL on ASP.NET Core 2 Identity?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2020-01-07
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多