Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net核心中使用返回url_C#_Asp.net Mvc_Asp.net Core_Asp.net Core Mvc_Asp.net Identity - Fatal编程技术网

C# 在asp.net核心中使用返回url

C# 在asp.net核心中使用返回url,c#,asp.net-mvc,asp.net-core,asp.net-core-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,Asp.net Identity,如果用户在访问特定URL时未经过身份验证/授权,我们将尝试将用户重定向(使用返回URL)到登录页面。但是,在将用户重定向到登录页面时,我们无法在路由中添加自定义参数(本例中为clientname)。我们正在使用asp.net身份核心框架 在Startup.cs中,我们定义了适用于所有人的以下路线 app.UseMvc(routes => { routes.MapRoute( name: "Edg

如果用户在访问特定URL时未经过身份验证/授权,我们将尝试将用户重定向(使用返回URL)到登录页面。但是,在将用户重定向到登录页面时,我们无法在路由中添加自定义参数(本例中为clientname)。我们正在使用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()));
})
并将
标识选项
配置为重定向到登录页面,如下所示

services.Configure<IdentityOptions>(opt =>
{
  opt.Cookies.ApplicationCookie.LoginPath = new PathString("/Account/Login");
});
http://localhost:5002/ClientA/Account/Login?ReturnUrl=/ClientA/home/index
如果用户试图在没有身份验证的情况下访问任何URL,它应该重定向到登录页面。以家庭控制器为例,考虑以下指标方法:
public IActionResult Index()
{
    return View();
}
但是,每当我们尝试将用户重定向到登录页面时,它都不会在URL中附加客户端名称。它形成于URL下方,在该URL中,
/Account/Login

http://localhost:5002/Account/Login?ReturnUrl=/ClientA/home/index
正因为如此,它导致404页面未找到错误。所以我们需要做什么更改才能正确重定向

Url的格式应如下所示

services.Configure<IdentityOptions>(opt =>
{
  opt.Cookies.ApplicationCookie.LoginPath = new PathString("/Account/Login");
});
http://localhost:5002/ClientA/Account/Login?ReturnUrl=/ClientA/home/index

您专门在身份验证选项上设置登录路径。默认情况下,无论您试图访问的资源是什么,当您未经身份验证时,它都会将您引导到那里。我相信您可能需要替换或继承/覆盖一些内部文件,以便使LoginPath基于您请求的资源而动态。我不确定动态登录路径是否本机支持,否则?我可能错了

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

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

您可以使用
Events
获取请求并重定向到您想要的url,如本示例所示

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;
                }
            };

        });

查看此链接:

看起来他们在.Net核心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");
}
公共异步任务登录(LoginViewModel模型,字符串returnUrl=”“)
{
……其他代码
如果(!String.IsNullOrEmpty(returnUrl)和&Url.islocalur(returnUrl))
返回重定向(returnUrl);
其他的
返回重定向到操作(“索引”、“主页”);
}
现在转到HTML代码:

@{
    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>
@{
ViewData[“标题”]=“登录”;
Layout=“~/Views/Shared/_Layout.cshtml”;
var returnUrl=@Context.Request.Query[“returnUrl”];
}

现在它工作得很顺利

在routeconfig.cs不存在的情况下,是否可以使用asp.net core发布整个routeconfig.csI am。我们在Startup.cs文件中定义了路由,我在codeYes中提供了该文件。我特别设置了LoginPath in authentication选项,因为我无法在登录路径中添加客户端名称,或者无法动态构建登录路径。这就是我的问题,我们应该怎么做?