302重定向到登录提供程序响应

时间:2019-06-24 07:19:00

标签: c# asp.net-core openid-connect

我正在使用ASPNET Core Web API 2.3和外部OpenId Provider。我希望我的API返回状态代码302和HTTP标头中重定向到的位置。

我需要我的应用程序(它是本机移动应用程序)才能获取URL并通过打开网页致电进行登录。因此,我不希望任何底层框架自动执行重定向。

我尝试覆盖OnRedirectToIdentityProvider回调

options.Events = new OpenIdConnectEvents
{
    OnRedirectToIdentityProvider = async (ctx) =>
    {
        if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200)
        {
            var service = await httpClientFactory.CreateClient()
                .GetDiscoveryDocumentAsync(apiOptions.Authority);

            var ru = new RequestUrl(service.AuthorizeEndpoint);
            var url = ru.CreateAuthorizeUrl(
                clientId: apiOptions.ClientId,
                responseType: "code",
                redirectUri: apiOptions.RedirectUri,
                scope: string.Join(" ", apiOptions.Scopes));

            ctx.Response.Redirect(url);
            ctx.HandleResponse();
        }
    }
}

但这将返回200 OK并且已经执行了重定向,该内容已经包含重定向目标的内容。

我在这里做什么错了?

0 个答案:

没有答案
相关问题