自定义 OAuth2 服务器 returns 401 - 未经授权

Custom OAuth2 server returns 401 - Unathorized

我正在尝试创建支持资源所有者密码凭据流的自定义 OAuth2 授权服务器。授权服务器是托管在IIS7.5中的WebAPI应用程序。

我已经配置了启动 class,我在其中注册了自定义 OAuthServerProvider (AtcAuthorizationServerProvider)。

[assembly: OwinStartup(typeof(ATC.WebApi.AuthorizationServer.Startup))]

namespace ATC.WebApi.AuthorizationServer
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureOAuth(app);

            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            app.UseWebApi(config);

            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        }

        public void ConfigureOAuth(IAppBuilder app)
        {
            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
                Provider = new AtcAuthorizationServerProvider(),
                RefreshTokenProvider = new AtcRefreshTokenProvider(),
                AuthenticationMode = AuthenticationMode.Passive
            };

            // Token Generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions(){});
        }
    }
}

在我的自定义提供程序 class 中,我重写了 ValidateClientAuthentication() 函数,我接受了两种客户端凭据接收方式(在 Body 和授权 header 中)。

public class AtcAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            string clientId = string.Empty;
            string clientSecret = string.Empty;

            // get client credentials from header or from body
            if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
            {
                context.TryGetFormCredentials(out clientId, out clientSecret);
            }
//rest of code

当我在 body 中发送 client_id 和 client_secret 时一切正常。

POST /ATC.WebApi.AuthorizationServer/token HTTP/1.1
Host: localhost
Accept: application/json
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache

grant_type=password&password=123456&username=myUser&client_id=myClient&client_secret=123%40abc

我成功获得访问令牌。

{
  "access_token": "3Fk_Ps10i45uL0zeCzIpvEh2WHKE8iJVNtKJ2XGWcQWXsT9jllKf...",
  "token_type": "bearer",
  "expires_in": 1799,
  "refresh_token": "4c1097d17dd14df5ac1c5842e089a88e",
  "as:client_id": "myClient"
}

但是,如果我使用 DotNetOpenAuth.OAuth2.WebServerClient,它在授权 client_id 和 client_secret 中通过 header,我将收到 401.1 - 未经授权 HTTP 响应。我发现 ValidateClientAuthentication() 没有被触发。

请求看起来像这样:

POST /ATC.WebApi.AuthorizationServer/token HTTP/1.1
Host: localhost
Accept: application/json
Content-Type: application/x-www-form-urlencoded
Authorization: Basic C16b34lUjEyM0BhYmM=
Cache-Control: no-cache

grant_type=password&password=123456&username=myUser

问题是在这种情况下如何说服 OWIN middle-ware 解雇我的自定义提供者?

嗯,终于知道问题出在哪里了。我的 IIS 允许基本身份验证,因此 IIS 收到请求并尝试对失败的用户进行身份验证,IIS 立即返回 401 Unauthorized。所以我的 OWIN 中间件甚至没有收到处理请求。