我收到错误说" CurlException:无法连接到服务器"
我正在尝试在客户端控制器中使用此代码访问API:
var tokenClient = new TokenClient("http://localhost:5003/connect/token", "client", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var content = await client.GetStringAsync("http://localhost:5102/api/catalog/items");
ViewBag.Json = JArray.Parse(content).ToString();
return View("json");
这是客户端中的代码:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
// options.DefaultAuthenticateScheme = "Cookies";
})
.AddCookie()
.AddOpenIdConnect(options => {
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = identityUrl.ToString();
options.SignedOutRedirectUri = callBackUrl.ToString();
options.ClientId ="client";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.RequireHttpsMetadata = false;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api1");
IdentityServer配置中的代码:
new Client
{
ClientId = "client",
ClientSecrets = new [] { new Secret("secret".Sha256())},
AllowedGrantTypes = GrantTypes.Hybrid,
RedirectUris = { "http://localhost:5202/signin-oidc" },
PostLogoutRedirectUris = {"http://localhost:5202/signout-callback-oidc"},
AllowAccessTokensViaBrowser = false,
AllowOfflineAccess = true,
RequireConsent = true,
AlwaysSendClientClaims = true,
AlwaysIncludeUserClaimsInIdToken = true,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
//"MobileApi"
"api1"
}
Api中的代码:
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority =Configuration.GetValue<string>("IdentityUrl");
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
所有Url变量都是正确的,我已经在其他地方使用它们而没有问题。我认为问题在于docker因为github上的这个帖子:https://github.com/aspnet/Home/issues/1975。然而,回答&#34;没有任何解释,执行起来也含糊不清。有没有办法解决这个问题,以便我的客户端可以通过docker连接到api?
答案 0 :(得分:0)
您需要使用容器的IP地址或主机名,而不是使用localhost。