【发布时间】:2021-09-25 02:31:29
【问题描述】:
大家好,我正在尝试使用身份服务器通过 apitwo 访问 apione,我确实收到了访问令牌,但无法使用它从 apitwo 接收秘密数据
你们能帮我吗? https://github.com/noahsalvadordenjo/cant-reach-secret-route 配置.cs
public static IEnumerable<ApiResource> GetApis() => new List<ApiResource>
{
new ApiResource("ApiOne"),
new ApiResource("ApiTwo"),
};
public static IEnumerable<Client> GetClients() => new List<Client>
{
new Client
{
ClientId = "client_id",
ClientSecrets = { new Secret("client_secret".ToSha256())},
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = { "ApiOne" }
}
};
public static IEnumerable<ApiScope> GetScopes() => new List<ApiScope>
{
new ApiScope("ApiOne"),
new ApiScope("ApiTwo")
};
身份服务器启动.cs
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddInMemoryClients(Configuration.GetClients())
.AddInMemoryApiResources(Configuration.GetApis())
.AddInMemoryApiScopes(Configuration.GetScopes())
.AddDeveloperSigningCredential();
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseIdentityServer();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
路由我试图从 apione secretcontroller.cs 访问
public class SecretController : Controller
{
[Route("/secret")]
[Authorize]
public string Index()
{
return "Secretmessage fropm apione";
}
}
Homecontroller api 1 尝试访问 api 2
public class HomeController : Controller
{
private readonly IHttpClientFactory httpClientFactory;
public HomeController(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
[Route("/")]
public async Task<IActionResult> Index()
{
var serverClient = httpClientFactory.CreateClient();
var discoveryDocument = await serverClient.GetDiscoveryDocumentAsync("https://localhost:44395/");
var token = await serverClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = discoveryDocument.TokenEndpoint,
ClientId = "client_id",
ClientSecret = "client_secret",
Scope = "ApiOne"
});
var apiClient = httpClientFactory.CreateClient();
apiClient.SetBearerToken(token.AccessToken);
var response = await apiClient.GetAsync("https://localhost:44368/secret");
var content = await response.Content.ReadAsStringAsync();
return Ok(new {
access_token = token.AccessToken,
message = content
});
}
}
Startup ApiOne
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", config =>
{
config.Authority = "https://localhost:44395/";
config.Audience = "ApiOne";
});
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
启动apitwo
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", config =>
{
config.Authority = "https://localhost:44395/";
config.Audience = "ApiTwo";
});
services.AddHttpClient();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
【问题讨论】:
-
复制粘贴相关代码到这里。见:stackoverflow.com/help/minimal-reproducible-example
标签: asp.net-core identityserver4