挖一点点后,我发现其中介绍了如何编程的方式验证通过使用JwtSecurityTokenHandler class的ADFS的OAuth 2.0认证流程制定的基于JWT认证令牌following answer。代码示例可以在链接的答案中找到。
这将允许我来创建自定义授权过滤器,其然后我可以作为控制器或控制器的方法的一个属性使用。此过滤器将分析客户端请求中的授权标头,检测其中包含的身份验证令牌的类型,然后启动相应的程序逻辑以验证/验证身份验证令牌。
沿东西,也许这些线路:
public enum AuthTokenType
{
OAuth2Bearer,
Custom
}
public class CustomAuthenticationAttribute : IAuthenticationFilter
{
public bool AllowMultiple
{
get
{
throw new NotImplementedException();
}
}
public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
HttpRequestMessage incommingRequest = context.Request;
HttpHeaders headers = incommingRequest.Headers;
string authHeader = GetHeader(headers, "Authorization");
AuthTokenType authTokenType = DetecteAuthTokenType(authHeader);
if (authTokenType == AuthTokenType.OAuth2Bearer)
{
// Validate auth token using the JwtSecurityTokenHandler class
}
else if (authTokenType == AuthTokenType.Custom)
{
// Validate auth token using whatever is necessary
}
else
{
// auth token doesn't correspond to a recognized type or hasn't been part of the client request - reject request
}
}
public AuthTokenType DetectAuthTokenType(string authHeader)
{
// Analyze the authorization header string and return its proper type
}
private string GetHeader(HttpHeaders headers, string key)
{
IEnumerable<string> keys = null;
if (!headers.TryGetValues(key, out keys))
return null;
return keys.First();
}
}
我不知道你的使用情况是什么,但,这似乎是一些认为thinktecture与identityserver3使用的例子。 https://identityserver.github.io/Documentation/。我花了一个周末的时间来了解一些例子,但是这很简单。我还添加了提供管理屏幕的identityreboot。 – Bill
嗨比尔。感谢提示。这绝对是我将考虑的事情,因为它可以[为auth令牌实现自定义验证](https://identityserver.github.io/Documentation/docsv2/configuration/serviceFactory.html)。 –