如何在 ASP.NET MVC 6 (ASP.NET Core) 中获取 returnUrl AccessDeniedPath

How to get in returnUrl AccessDeniedPath in ASP.NET MVC 6 (ASP.NET Core)

我使用的是最新版本的ASP.NET MVC 6.

设置了以下设置 Startup.cs 文件:

public void ConfigureServices(IServiceCollection services)
{
            services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
            {
                options.Cookies.ApplicationCookie.LoginPath = new PathString("/account/login");
                options.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/error/accessdenied");
                options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(1);
                options.Cookies.ApplicationCookie.SlidingExpiration = false;
                options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
                options.Cookies.ApplicationCookie.AutomaticChallenge = true;
            })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
}

Account控制器有动作Login

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

在上面的方法中我没有问题得到 returnUrl.

也在控制器中Error有动作AccessDenied

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

但是当用户没有权限访问该站点的页面时,系统将用户转发到一个页面../error/accessdenied

目前,在我被重定向后 returnUrl 值是 null

我能否获取用户被重定向的页面地址(以及它适用于 LoginPath)?

好像会在RC2中发布,看看GitHub

上的CookieAuthorizationHandler.HandleForbiddenAsync方法源码
protected override async Task<bool> HandleForbiddenAsync(ChallengeContext context)
{
    var properties = new AuthenticationProperties(context.Properties);
    var returnUrl = properties.RedirectUri;
    if (string.IsNullOrEmpty(returnUrl))
    {
        returnUrl = OriginalPathBase + Request.Path + Request.QueryString;
    }
    var accessDeniedUri = Options.AccessDeniedPath + QueryString.Create(Options.ReturnUrlParameter, returnUrl);
    var redirectContext = new CookieRedirectContext(Context, Options, BuildRedirectUri(accessDeniedUri), properties);
    await Options.Events.RedirectToAccessDenied(redirectContext);
    return true;
}

您可以在 Home repository 上获得有关如何从 RC1 迁移到 RC2 的反馈。