使用Okta登录ASP.NET Core后将用户重定向到默认页面 [英] Redirecting user to default page after login in ASP.NET Core using Okta

查看:145
本文介绍了使用Okta登录ASP.NET Core后将用户重定向到默认页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在ConfigureServices中:

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"
                };
            });

我的登录操作:

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

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

IIRC,我要寻找的是ASP.NET的FormsAuthentication配置中的 defaultUrl 设置.

IIRC, what I'm looking for is the equivalent of the defaultUrl setting in the FormsAuthentication configuration in ASP.NET.

推荐答案

当您要求登录时,ASP.NET Core中的新模式是在 AuthenticationProperties 中指定登录后目标:

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.

全面披露:我在Okta工作,并建立了许多.NET库和示例.我们需要对此做更好的记录,我会确保做到这一点!

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!

这篇关于使用Okta登录ASP.NET Core后将用户重定向到默认页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆