问题
I am using Okta for authentication in my ASP.NET Core application. After login, I would like to redirect the user to a different page, but I cannot find where to configure this.
In ConfigureServices:
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"
};
});
My login action:
public IActionResult Login()
{
if (!HttpContext.User.Identity.IsAuthenticated)
{
return Challenge(OpenIdConnectDefaults.AuthenticationScheme);
}
return RedirectToAction("Index", "Home");
}
IIRC, what I'm looking for is the equivalent of the defaultUrl
setting in the FormsAuthentication configuration in ASP.NET.
回答1:
The new pattern in ASP.NET Core is to specify a post-login destination in the AuthenticationProperties
when you challenge for login:
var properties = new AuthenticationProperties
{
RedirectUri = "/logged-in"
};
return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);
It also works the same way during logout.
Full disclosure: I work at Okta and built a lot of our .NET libraries and samples. We need to document this better, I'll make sure we do!
来源:https://stackoverflow.com/questions/51191808/redirecting-user-to-default-page-after-login-in-asp-net-core-using-okta