2016-07-15 89 views
3

我刚刚完成this excellent article有关反对使用ADAL和OWIN中间件组件的ADFS/Windows Azure的AD例如确保与OAuth2用户的ASP.NET Web API 2应用程序。是否可以针对多个身份验证提供程序保护ASP.NET Web API 2应用程序?

然而,似乎在这篇文章中描述的整个认证工作流是非常“硬连线”到HTTP请求管道,并且不留任何余地与其他认证供应商认证流程的执行情况。

这是为什么需要?

我有一个移动Web客户端中,“内部”和“外部”允许用户以发出针对API端点用户相关的数据的请求进行身份验证。

虽然“内部”用户正在从Azure AD/ADFS获取身份验证令牌,但“外部”用户必须对另一个颁发另一种身份验证令牌的系统进行身份验证。

因此,我必须能够在API端点级别上区分来自“内部”和“外部”用户的请求,以启动其不同验证令牌的正确评估工​​作流程。

就如何实现这一目标的任何迹象将高度赞赏。

问候,马蒂亚斯

+1

我不知道你的使用情况是什么,但,这似乎是一些认为thinktecture与identityserver3使用的例子。 https://identityserver.github.io/Documentation/。我花了一个周末的时间来了解一些例子,但是这很简单。我还添加了提供管理屏幕的identityreboot。 – Bill

+0

嗨比尔。感谢提示。这绝对是我将考虑的事情,因为它可以[为auth令牌实现自定义验证](https://identityserver.github.io/Documentation/docsv2/configuration/serviceFactory.html)。 –

回答

0

挖一点点后,我发现其中介绍了如何编程的方式验证通过使用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(); 
    } 
} 
相关问题