Identity Server 3 在启动调试模式时联系 Web 服务器挂起
Identity Server 3 Contacting the web server hangs when launching debug mode
我正在研究 API,它需要为具有正确权限的用户保护某些端点。我正在使用 IdentityServer3 并遵循这个 pluralsight 课程:https://app.pluralsight.com/library/courses/oauth-secure-asp-dot-net-api/table-of-contents
我已经完成了创建自签名证书并将其加载为我的签名证书的步骤。我的 API 和 Auth 服务器都在同一个 .NET 项目中。
在 startup.cs
文件中,如果我使用此代码配置接受传入令牌的方式,应用程序工作正常,我可以访问具有 [Authorize]
属性的端点:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServer(CreateIdentityServerOptions());
var cert = new x509certificate2(
convert.frombase64string("My Certificate Public Key")
);
app.usejwtbearerauthentication(new jwtbearerauthenticationoptions
{
allowedaudiences = new[] { "http://localhost/MyProject/resources" },
tokenvalidationparameters = new tokenvalidationparameters
{
validaudience = "http://localhost/MyProject/resources",
validissuer = "http://localhost/MyProject",
issuersigningkey = new x509securitykey(cert)
}
});
}
//Other Code, such as CreateIdentityServerOptions(), goes here.
}
因此我可以在此端点内设置断点:
[HttpGet]
[Authorize]
public IHttpActionResult GetUser()
{
var claimsPrincipal = User as ClaimsPrincipal;
var userName = claimsPrincipal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
var userId = GetUserId(userName);
return Ok();
}
但是 如果我继续学习课程并到达它使用 IdentityServer3.AccessTokenValidation
来简化代码的部分,如下所示:
public void Configuration(IAppBuilder app)
{
app.UseIdentityServer(CreateIdentityServerOptions());
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
{
Authority = "http://localhost/MyProject"
});
}
此更改后,当我启动应用程序时,我在 visual studio 中看到一个弹出窗口,上面写着 "Contacting the web server to start debugging",它挂起几分钟,然后放弃并退出。应用程序未启动。
我怀疑这是因为我在同一个项目中同时拥有Auth provider和Auth consumer的逻辑,所以本质上是在等待项目开始抓取public 键...所以它可以启动。但我想确保在选择如何继续前进之前了解问题。
如果 IdentityServer 和令牌消费者托管在同一应用程序中,则在获取发现文档时可能会出现竞争条件。
对于这些情况,将访问令牌验证中间件上的 DelayLoadMetadata
属性 设置为 true
。
我正在研究 API,它需要为具有正确权限的用户保护某些端点。我正在使用 IdentityServer3 并遵循这个 pluralsight 课程:https://app.pluralsight.com/library/courses/oauth-secure-asp-dot-net-api/table-of-contents
我已经完成了创建自签名证书并将其加载为我的签名证书的步骤。我的 API 和 Auth 服务器都在同一个 .NET 项目中。
在 startup.cs
文件中,如果我使用此代码配置接受传入令牌的方式,应用程序工作正常,我可以访问具有 [Authorize]
属性的端点:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServer(CreateIdentityServerOptions());
var cert = new x509certificate2(
convert.frombase64string("My Certificate Public Key")
);
app.usejwtbearerauthentication(new jwtbearerauthenticationoptions
{
allowedaudiences = new[] { "http://localhost/MyProject/resources" },
tokenvalidationparameters = new tokenvalidationparameters
{
validaudience = "http://localhost/MyProject/resources",
validissuer = "http://localhost/MyProject",
issuersigningkey = new x509securitykey(cert)
}
});
}
//Other Code, such as CreateIdentityServerOptions(), goes here.
}
因此我可以在此端点内设置断点:
[HttpGet]
[Authorize]
public IHttpActionResult GetUser()
{
var claimsPrincipal = User as ClaimsPrincipal;
var userName = claimsPrincipal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
var userId = GetUserId(userName);
return Ok();
}
但是 如果我继续学习课程并到达它使用 IdentityServer3.AccessTokenValidation
来简化代码的部分,如下所示:
public void Configuration(IAppBuilder app)
{
app.UseIdentityServer(CreateIdentityServerOptions());
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
{
Authority = "http://localhost/MyProject"
});
}
此更改后,当我启动应用程序时,我在 visual studio 中看到一个弹出窗口,上面写着 "Contacting the web server to start debugging",它挂起几分钟,然后放弃并退出。应用程序未启动。
我怀疑这是因为我在同一个项目中同时拥有Auth provider和Auth consumer的逻辑,所以本质上是在等待项目开始抓取public 键...所以它可以启动。但我想确保在选择如何继续前进之前了解问题。
如果 IdentityServer 和令牌消费者托管在同一应用程序中,则在获取发现文档时可能会出现竞争条件。
对于这些情况,将访问令牌验证中间件上的 DelayLoadMetadata
属性 设置为 true
。