身份服务器 - 不记名令牌认证 - 如何跟踪失败的授权?
Identity server - bearer token authentication - how to trace failed authorization?
我已经在我的开发环境中设置了一个简单的身份服务器,配置如下:
public void Configuration(IAppBuilder app)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Trace()
.CreateLogger();
app.Map("/identity", id =>
{
id.UseIdentityServer(new IdentityServerOptions()
{
SiteName = "Tomas Services Identity Provider",
SigningCertificate = CertificateService.Load(),
Factory = IdentityFactory.Configure("IdServerConn"),
RequireSsl = false
});
});
}
工厂根据编写服务器代码的好人提供的 entity framework 示例进行设置。
然后我有一个客户端网站 api 站点设置为使用不记名身份验证,如下所示:
private const string IdentityServerUrl = "http://localhost/mysite/identity";
public void Configuration(IAppBuilder app)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Trace()
.CreateLogger();
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
{
Authority = IdentityServerUrl,
RequiredScopes = new[] { "my_scope" }
});
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseNinjectMiddleware(NinjectWebCommon.CreateKernel).UseNinjectWebApi(config);
}
然后我使用 fiddler 对此进行测试,检索访问令牌,然后将其添加到我的网站 api POST.
中的适当 header
现在,这是在 https 下使用 IIS express 和 运行ning 工作的。我想在没有 SSL 的情况下将此(用于开发)更改为 运行。所做的唯一更改是在 IDP 配置中将 RequiresSSL 设置为 false 并更改客户端中权限的 URL。客户端现在也在 http 而不是 SSL 下 运行ning。
我现在一直收到 401 - 在发布到网络时未经授权的响应 API。我可以毫无问题地检索访问令牌,但我不明白为什么我的网站 API 没有对我进行身份验证。
我在身份服务器站点上设置了日志记录,我可以看到通过调用获取我的访问令牌没问题,但是当我通过网络调用时,我看到的唯一进一步的日志记录 API 是一次:
w3wp.exe Information: 0 : 2016-10-17 15:09:58.459 +01:00 [Information]
Start discovery request 2016-10-17 15:09:58.460 +01:00 [Debug] Cache
miss: CachingScopeStore.allscopes.public w3wp.exe Information: 0 :
2016-10-17 15:09:58.549 +01:00 [Information] Start key discovery
request
是否有任何方法可以从 UseIdentityServerBearerTokenAuthentication
OWIN 中间件获取进一步的日志记录(用于调试目的)?我不知道为什么我无法连接,特别是因为这是在 IIS express 下工作(在 SSL 下)。
为了后代,我在按照下面的@leastprivilege 启用日志记录时遇到的错误是:
Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware
Error: 0 : Authentication failed System.TypeLoadException: Could not
load type 'IdentityModel.Extensions.HashStringExtensions' from
assembly 'IdentityModel, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=null'. at
IdentityServer3.AccessTokenValidation.ValidationEndpointTokenProvider.d__1.MoveNext()
at
System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine&
stateMachine) at
IdentityServer3.AccessTokenValidation.ValidationEndpointTokenProvider.ReceiveAsync(AuthenticationTokenReceiveContext
context) at
Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.d__0.MoveNext()
详情请看下面的回答。
文档展示了如何为令牌验证启用日志记录(在 API 项目中)
https://identityserver.github.io/Documentation/docsv2/consuming/diagnostics.html
因此,找出实际错误后,这完全是我的错。尽管阅读了非常清晰的文档和示例,但我为我的解决方案引用了错误的 'IdentityModel' nuget 包。
因为我使用的是 'older' .net,所以我不应该使用 IdentityModel2(即 IdentiyModel nuget 包的 v2.0.0.0)。降级到 v1.13 已经解决了这个问题,我的解决方案现在按预期工作。
我已经在我的开发环境中设置了一个简单的身份服务器,配置如下:
public void Configuration(IAppBuilder app)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Trace()
.CreateLogger();
app.Map("/identity", id =>
{
id.UseIdentityServer(new IdentityServerOptions()
{
SiteName = "Tomas Services Identity Provider",
SigningCertificate = CertificateService.Load(),
Factory = IdentityFactory.Configure("IdServerConn"),
RequireSsl = false
});
});
}
工厂根据编写服务器代码的好人提供的 entity framework 示例进行设置。
然后我有一个客户端网站 api 站点设置为使用不记名身份验证,如下所示:
private const string IdentityServerUrl = "http://localhost/mysite/identity";
public void Configuration(IAppBuilder app)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Trace()
.CreateLogger();
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
{
Authority = IdentityServerUrl,
RequiredScopes = new[] { "my_scope" }
});
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseNinjectMiddleware(NinjectWebCommon.CreateKernel).UseNinjectWebApi(config);
}
然后我使用 fiddler 对此进行测试,检索访问令牌,然后将其添加到我的网站 api POST.
中的适当 header现在,这是在 https 下使用 IIS express 和 运行ning 工作的。我想在没有 SSL 的情况下将此(用于开发)更改为 运行。所做的唯一更改是在 IDP 配置中将 RequiresSSL 设置为 false 并更改客户端中权限的 URL。客户端现在也在 http 而不是 SSL 下 运行ning。
我现在一直收到 401 - 在发布到网络时未经授权的响应 API。我可以毫无问题地检索访问令牌,但我不明白为什么我的网站 API 没有对我进行身份验证。
我在身份服务器站点上设置了日志记录,我可以看到通过调用获取我的访问令牌没问题,但是当我通过网络调用时,我看到的唯一进一步的日志记录 API 是一次:
w3wp.exe Information: 0 : 2016-10-17 15:09:58.459 +01:00 [Information] Start discovery request 2016-10-17 15:09:58.460 +01:00 [Debug] Cache miss: CachingScopeStore.allscopes.public w3wp.exe Information: 0 : 2016-10-17 15:09:58.549 +01:00 [Information] Start key discovery request
是否有任何方法可以从 UseIdentityServerBearerTokenAuthentication
OWIN 中间件获取进一步的日志记录(用于调试目的)?我不知道为什么我无法连接,特别是因为这是在 IIS express 下工作(在 SSL 下)。
为了后代,我在按照下面的@leastprivilege 启用日志记录时遇到的错误是:
Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware Error: 0 : Authentication failed System.TypeLoadException: Could not load type 'IdentityModel.Extensions.HashStringExtensions' from assembly 'IdentityModel, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'. at IdentityServer3.AccessTokenValidation.ValidationEndpointTokenProvider.d__1.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at IdentityServer3.AccessTokenValidation.ValidationEndpointTokenProvider.ReceiveAsync(AuthenticationTokenReceiveContext context) at Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.d__0.MoveNext()
详情请看下面的回答。
文档展示了如何为令牌验证启用日志记录(在 API 项目中)
https://identityserver.github.io/Documentation/docsv2/consuming/diagnostics.html
因此,找出实际错误后,这完全是我的错。尽管阅读了非常清晰的文档和示例,但我为我的解决方案引用了错误的 'IdentityModel' nuget 包。
因为我使用的是 'older' .net,所以我不应该使用 IdentityModel2(即 IdentiyModel nuget 包的 v2.0.0.0)。降级到 v1.13 已经解决了这个问题,我的解决方案现在按预期工作。