【发布时间】:2017-01-09 22:50:36
【问题描述】:
我正在开发一个 API,该 API 需要为具有正确权限的用户保护某些端点。我正在使用 IdentityServer3 并遵循这个复数课程:https://app.pluralsight.com/library/courses/oauth-secure-asp-dot-net-api/table-of-contents
我已经完成了创建自签名证书并将其加载为我的签名证书的步骤。我的 API 和身份验证服务器都在同一个 .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 中收到一个弹出窗口,上面写着“正在联系 Web 服务器以开始调试”,它会挂起几分钟,然后放弃并退出。应用程序没有启动。
我怀疑这是因为我在同一个项目中拥有 Auth 提供者和 Auth 消费者的逻辑,因此它本质上是在等待项目开始获取公钥。 . 所以它可以开始了。但我想确保在选择如何继续之前了解问题所在。
【问题讨论】:
-
日志记录呢?日志是怎么说的?
标签: c# asp.net oauth-2.0 identityserver3