使用 Okta 在 ASP.NET Core 中登录后将用户重定向到默认页面

Redirecting user to default page after login in ASP.NET Core using Okta

我在 ASP.NET 核心应用程序中使用 Okta 进行身份验证。登录后,我想将用户重定向到不同的页面,但我找不到在哪里配置它。

在配置服务中:

    services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.ClientId = "<clientid>";
                options.ClientSecret = configuration.OktaClientSecret;
                options.Authority = "https://dev-460010-admin.oktapreview.com/oauth2/default";
                options.CallbackPath = "/authorization-code/callback";
                options.ResponseType = "code";
                options.SaveTokens = true;
                options.UseTokenLifetime = false;
                options.GetClaimsFromUserInfoEndpoint = true;
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name"
                };
            });

我的登录操作:

    public IActionResult Login()
    {
        if (!HttpContext.User.Identity.IsAuthenticated)
        {
            return Challenge(OpenIdConnectDefaults.AuthenticationScheme);
        }

        return RedirectToAction("Index", "Home");
    }

IIRC,我正在寻找的是 ASP.NET.

中 FormsAuthentication 配置中的 defaultUrl 设置的等效项

ASP.NET核心中的新模式是在您挑战登录时在AuthenticationProperties中指定一个post-登录目的地:

var properties = new AuthenticationProperties
{
    RedirectUri = "/logged-in"
};

return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);

注销时同样如此。

完全披露:我在 Okta 工作并构建了很多我们的 .NET 库和示例。我们需要更好地记录这一点,我会确保我们做到!