在 AuthenticationHttpContextExtensions.AuthenticateAsync() 中为方案放置什么

问题描述 投票:0回答:1

我正在使用 OIDC 和 Identity Server 4,它正在使用 Okta 进行身份验证。

我在回调方法中调用

var result = await HttpContext.AuthenticateAsync("Identity.External");

我选择

Identity.External
作为方案,因为我注意到这是对回调方法的请求中 cookie 的名称:

但是,我意识到我可以使用

Startup.ConfigureServices()
中的这段代码重命名这个 cookie:

    services.ConfigureExternalCookie(config =>
    {                
        config.Cookie.Name = "test12";
    });

但是重命名cookie后,对

HttpContext.AuthenticateAsync("Identity.External")
的调用仍然有效,所以看起来scheme名称与这个cookie名称无关。

我们怎么知道要放什么字符串值?

某处有可接受的值列表吗?

这是我的

Startup.ConfigureServices()

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; // "Cookies"
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultSignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("oidc", "OpenIdConnect", options =>
{
    options.Authority = "oktaUrlHere";
    options.ClientId = "clientIdHere";
    options.ClientSecret = "clientSecretHere";
    options.SaveTokens = true;
    options.ResponseType = "code";
    options.Scope.Add("groups");
    options.Scope.Add("email");
    options.Events = new CustomOpenIdConnectEvents
    {
        ...
    };
});

更新:

我试着在方案前加上“1”只是为了看看会发生什么:

var result = await HttpContext.AuthenticateAsync("1Identity.External");

它返回此错误,其中包含已注册方案的列表:

An unhandled exception occurred while processing the request.
InvalidOperationException: No authentication handler is registered for the scheme '1Identity.External'.

The registered schemes are: Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId, idsrv, idsrv.external, Cookies, oidc. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("1Identity.External",...)?

这些方案都是默认注册的吗?

这在任何地方都有记录吗?

更新:

我在下面的代码中打了一个断点来查看

options
上的属性值:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    options.DefaultSignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
})

我可以看到

DefaultAuthenticateScheme
的默认值是
Identity.Application
DefaultSignInScheme
的默认值是
Identity.External

由于

options.DefaultAuthenticateScheme
有值,所以不会使用
options.DefaultScheme
"Cookies"
)。

根据msdn

DefaultAuthenticateScheme
是:

AuthenticateAsync(HttpContext, String) 用作默认方案。

如果是这样,为什么传递给

AuthenticateAsync()
的方案需要是
DefaultSignInScheme
"Identity.External"
)而不是
DefaultAuthenticateScheme
"Identity.Application"
)的值?

oauth-2.0 identityserver4 .net-6.0 openid-connect okta-api
1个回答
1
投票

cookie 的名称与您要执行的操作无关。

在你的客户端配置中,你通常有这样的东西:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(opt =>
{
   ...

}).AddOpenIdConnect(options =>
{
   ...
});

你在这个方法里放了什么

HttpContext.AuthenticateAsync("Identity.External") 

是方案名称。

但是,通常情况下,当您使用 OpenIDConnect 时,您希望通过 OpenIDConnect 处理程序来挑战用户,因此如果您想要实现的是要求用户登录,那么您应该使用这样的东西:

HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme)
© www.soinside.com 2019 - 2024. All rights reserved.