【发布时间】:2019-01-23 15:48:53
【问题描述】:
我创建了 2 个托管在 Azure 中的 Web 应用程序。一个应用程序 (A) 是 IdentityServer4 提供程序,另一个 (B) 是通过 OpenId Connect 使用 A 作为外部登录提供程序的应用程序。在 A 的代码中,我将 IdentityServer4 配置为要求 B 的登录调用使用特定的重定向 URI:
namespace A.IdentityServer4Config
{
public static class Clients
{
public static IEnumerable<Client> GetClients(IConfiguration configuration)
{
return new List<Client>
{
new Client
{
ClientId = "B",
ClientSecrets = {new Secret("hashed secret for B")},
AllowedGrantTypes = GrantTypes.CodeAndClientCredentials,
AllowedScopes = {"openid", "profile", ...},
RedirectUris = {"https://B.azurewebsites.net/Account/oidc-callback"},
RequireConsent = false,
AccessTokenLifetime = 10 * 60,
},
};
}
}
}
在 Startup.ConfigureServices 中:
var identityServerBuilder = services
.AddIdentityServer(options => {...})
...
.AddInMemoryClients(Clients.GetClients(configuration))
...;
请注意,我指定的重定向 URI 是 HTTPS URI。
在 B 的代码中,我使用 Microsoft.Extensions.DependencyInjection.OpenIdConnectExtensions 中的 AddOpenIdConnect() 重载之一来配置登录调用应如何进行:
services.AddAuthentication(options => {...})
...
.AddOpenIdConnect(
"oidc",
options =>
{
...
options.Authority = "https://A.azurewebsites.net";
options.RequireHttpsMetadata = true;
options.ClientId = "B";
options.ClientSecret = "secret for B";
options.ResponseType = "code";
options.CallbackPath = "/Account/oidc-callback"; // !!!
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
...
}
);
用“!!!”注释的行是配置登录请求的重定向 URI 的位置。请注意,它必须是相对于应用程序域的路径。您不能完整地指定 URI(我尝试过;如果您这样做,则应用程序在启动时会崩溃)。由于您无法完整指定 URI,因此该属性不能用于要求重定向 URI 为 HTTPS。 (RemoteAuthenticationOptions.CallbackPath 是一个Microsoft.AspNetCore.Http.PathString。在官方文档中描述如下:“应用程序的基本路径中的请求路径,用户代理将被返回。中间件将在它到达时处理这个请求.")
当我尝试在 B 登录时,我被带到 A 上的错误页面。当我在 A 上查看 IdentityServer4 生成的日志时,很明显错误的原因是请求的重定向 URI B 与预期的不匹配。两个 URI 之间的唯一区别是 A 期望看到的是 HTTPS URI,而 B 指定的是 HTTP(没有“S”)URI:
2018-08-16 15:20:41.307 +00:00 [Error] IdentityServer4.Endpoints.AuthorizeEndpoint: Request validation failed
2018-08-16 15:20:41.307 +00:00 [Information] IdentityServer4.Endpoints.AuthorizeEndpoint: {
"ClientId": "B",
"AllowedRedirectUris": [
"https://B.azurewebsites.net/Account/oidc-callback"
],
"SubjectId": "anonymous",
"RequestedScopes": "",
"Raw": {
"client_id": "B",
"redirect_uri": "http://B.azurewebsites.net/Account/oidc-callback",
"response_type": "code",
"scope": "openid profile ...",
"response_mode": "form_post",
"nonce": "[omitted]",
"state": "[omitted]",
"x-client-SKU": "ID_NETSTANDARD1_4",
"x-client-ver": "5.2.0.0"
}
}
2018-08-16 15:20:41.307 +00:00 [Information] IdentityServer4.Events.DefaultEventService: {
"Name": "Token Issued Failure",
"Category": "Token",
"EventType": "Failure",
"Id": 2001,
"ClientId": "B",
"Endpoint": "Authorize",
"Scopes": "",
"Error": "unauthorized_client",
"ErrorDescription": "Invalid redirect_uri",
"ActivityId": "[omitted]",
"TimeStamp": "2018-08-16T15:20:41Z",
"ProcessId": 10408,
"LocalIpAddress": "[omitted]",
"RemoteIpAddress": "[omitted]"
}
我不知道为什么 B 对 A 的请求默认为 HTTP 的重定向 URI。我不知道如何改为使用 HTTPS。我可以放宽重定向 URI 是 HTTPS 的要求,但这似乎是错误的做法。我想知道如何让 B 在其对 A 的请求中指定 HTTPS URI。根据文档中 RemoteAuthenticationOptions.CallbackPath 的描述,我认为这应该通过确保“应用程序的基本路径”是 HTTPS 来实现;但是,我不知道该怎么做,也没有成功搜索互联网。
当我在本地运行这些应用程序时,一切正常,因为当 B 在本地运行时,它发出的请求指定了一个 HTTPS 重定向 URI(如我所愿)。我不知道为什么它在本地运行和在 Azure 上运行时表现不同。
我尝试过的
如上所述,我尝试完全指定重定向 URI(而不是相对而言)。这不起作用 - 它会导致应用程序在启动时崩溃。
我还尝试将 CallbackPath 类型设置为使用 PathString.FromUriComponent() 构造的 PathString(使用各种绝对 URI)。这没有用。如果我将相对 URI 作为字符串传递,则 URI 的协议和域部分将被丢弃并替换为它们本来的样子(即“http”和相应的域)。
我查看了在https://github.com/IdentityServer/IdentityServer4.Samples 找到的各种示例 MVC 客户端中的 Startup 类。我没有看到任何明显可以解决此问题的方法。
【问题讨论】:
标签: azure asp.net-core https identityserver4 openid-connect