授权失败,带有自定义身份验证方案-asp.net-core 3.0

时间:2019-09-10 08:32:35

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

我正在使用:

services.AddAuthentication().AddCookie("custom", options=> {
                options.LoginPath = "/admin/in";
                options.LogoutPath = "/admin/out";
            });

ConfigureServices中注册自定义身份验证方案。

当我尝试在控制器中使用此方案时:

[Authorize(Roles = "admin", AuthenticationSchemes = "custom")]
        public ActionResult Index()
        {
            return View();
        }

它只是将我重定向到登录页面,即使用户已登录并且角色正确。

现在我是否将AuthenticationScheme更改为:

[Authorize(Roles = "admin", AuthenticationSchemes = "Identity.Application")]
        public ActionResult Index()
        {
            return View();
        }

一切正常,并且用户已获得授权。

自定义身份验证方案的设置中是否缺少某些内容?我如何使我的自定义身份验证方案以与Identity.Application相同的方式工作?

谢谢

ps:我正在使用asp.net core 3.0预览版9

1 个答案:

答案 0 :(得分:0)

设置options.ForwardAuthenticate = "Identity.Application";似乎可以解决此问题:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication().AddCookie("custom", options=> {
                options.LoginPath = "/admin/in";
                options.LogoutPath = "/admin/out";
                options.AccessDeniedPath= "/admin/in";
                options.ForwardAuthenticate = "Identity.Application";
            });

        }

这确实应该记录在案