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

坚强是说给别人听的谎言 提交于 2021-01-28 13:42:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!