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



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

在"配置服务"中:

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中指定登录后目标:

var properties = new AuthenticationProperties
{
RedirectUri = "/logged-in"
};
return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);

它在注销期间的工作方式也相同。

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

最新更新