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

标签 c# asp.net-mvc asp.net-core asp.net-core-mvc asp.net-core-1.0

我使用最新版本的 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();
}

帐户 Controller 中具有操作登录

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

在上面的方法中,我获取 returnUrl 没有问题。

此外,在 Controller 中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 的反馈。 .

关于c# - 如何在 ASP.NET MVC 6 (ASP.NET Core) 中获取 returnUrl AccessDeniedPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36492624/

相关文章:

c# - 提取网格或点云的中心曲线(中轴或拓扑骨架)的代码?

asp.net-core - 禁用 asp.net core 3.1 项目的 web.config 生成

c# - Nullable<T> 的装箱/拆箱行为如何可能?

c# - WPF - 删除启动后自动运行的应用程序

c# - 如何获取 WPF 列表框中选定项的索引?

css - Chekbox MVC 未发回数据。使用 Html.Chekboxfor 使用 proto.io 不工作

c# - 我可以为 ViewBag 提供扩展方法吗?

asp.net-mvc - DropDownListFor 枚举不会绑定(bind)回模型

asp.net-core - 在 MVC View /Razor 页面中使用 RenderComponentAsync (Blazor WebAssembly)

c# - 如何在 ASP.NET Core 中使用 HttpContextAccessor 读取 URI 参数