我目前正致力于创建.net核心2.1解决方案,以移动我们的团队代码,同时确保我们使用的是最新版本的库 . 但是,这些部分必须在以后完成,例如使用IdentityServer3的身份服务器 .
鉴于IdentityServer4已经存在,但我们还没有完全转向,我正在尝试使用IdentityServer4.AccessTokenValidation启动并运行我们的新web api项目,并将其连接到现有的IdentityServer3 .
现在,现有的web api项目甚至没有使用IdentityServer3.AccessTokenValidation,他们编写了自定义属性和与IdentityServer3的通信来处理令牌/范围的authing . 这与我所看到的核心中的最佳实践相反,核心不仅仅是做范围了,他们做的政策有范围......
新的.NET Core Web API已在startup.cs中设置如下:
//ConfigureServers(IServiceCollection serviceCollection)
var mvcBuilder = serviceCollection.AddMvcCore();
mvcBuilder.AddApiExplorer();
mvcBuilder.AddJsonFormatters();
mvcBuilder.AddAuthorization();
mvcBuilder.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//...
var athentication = serviceCollection.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme);
authentication.AddIdentityServerAuthentication(options => {
options.Authority = "https://www.example.com/api/connect/identity";
options.ApiName = null;
options.ApiSecret = null;
options.RequireHttpsMetadata = false;
options.LegacyAudienceValidation = true;
});
serviceCollection.AddAuthorization(options => {
options.AddPolicy("TempPrivaleges", builder => {
builder.RequireScope("TempScope");
};
});
//Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseHttpsRedirection();
app.UseMvc();
app.UseAuthentication();
控制器的设置如下:
[Authorize(Policy="TempPrivaleges")]
[Produces("application/json")]
[Route("api/[controller]/[action]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public ActionResult<string> SayHello()
{
return Ok("Hello");
}
[HttpGet]
public ActionResult<IEnumerable<string>> Test()
{
return Ok(new string[] { "Test1", "Test2" });
}
}
但是,当我使用SOAP UI获取令牌时,然后使用所述令牌针对我的API发出请求,我总是未经授权 .
我用来获取令牌的URL如下:https://www.example.com/api/connect/identity/connect/token
现有API使用以下内容进行令牌验证:https://www.example.com/api/connect/identity/connect/accesstokenvalidation
我不清楚的关键事项是Startup.cs中的API和API Secret . 我不确定这些 Value 应该是什么 . 当现有API调用验证时,它们不提供任何此类操作 . 我也不确定我是否正确设置了权限地址 .
我也找不到任何有关为API端打开登录的文档,以查看它正在尝试执行的操作 . 遗憾的是,我无法访问正在运行的IdentityServer3来为其启用登录 .
任何人都可以提供任何帮助将不胜感激 .