首页 文章

如何通过Owin OAuthBearerAuthentication验证访问令牌?

提问于
浏览
9

What I want:

  • 令牌生成器使用OAuthAuthorizationServer和令牌使用者使用OAuthBearerAuthentication(验证访问令牌) .

  • 使用OWIN管道管理所有内容,令牌内容和web api内容 .

What about the code:

public void Configuration(IAppBuilder app)
{
    app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
    {
        AuthorizeEndpointPath = "/Authorize",
        AllowInsecureHttp = true,
        Provider = new OAuthAuthorizationServerProvider 
        {
            OnGrantCustomExtension = GrantCustomExtension,
            OnValidateClientRedirectUri = ValidateClientRedirectUri,
            OnValidateClientAuthentication = ValidateClientAuthentication,
        }
    });

    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
    {
        Provider = new OAuthBearerAuthenticationProvider 
        { 
            //Handles applying the authentication challenge to the response message.
            ApplyChallenge=MyApplyChallenge,

            //Handles processing OAuth bearer token.
            RequestToken=MyRequestToken,

            //Handles validating the identity produced from an OAuth bearer token.
            ValidateIdentity = MyValidateIdentity,
        }
    });

    app.UseWebApi(new WebApplication3.Config.MyWebApiConfiguration());
}

What's the question:

  • OAuthBearerAuthenticationProviderApplyChallenge ,_28863382_和 ValidateIdentity 的3个属性 . 如何实现3方法?

  • 在令牌认证过程中,我想到的是解密访问令牌,验证来自客户端的令牌,如果令牌已经过验证,则将令牌的身份放入 HttpContext.Current.User .

OAuthBearerAuthenticationProvider 的责任是完成前面的步骤 . 我对吗?

1 回答

  • 5

    如您所知,UseOAuthAuthorizationServer具有验证用户身份的工作 . 然后,UseOAuthBearerAuthentication的工作是确保只有经过身份验证的用户才能访问您的应用程序 . 通常,这两个作业被分配给不同的Web应用程序 . 看起来你的应用程序正在做这两件事 .

    当然有些情况下您需要覆盖默认的OAuthBearerAuthenticationProvider . 也许你这样做,或者你可能没有 . 在我的情况下,ApplicationCookie并不适合这种情况 . 因此,我将第三方JWT令牌存储在cookie中,而不是标头中,并使用它来指示用户已通过Web应用程序的身份验证 . 我还需要重定向到我自己的登录页面,而不是提供401 .

    这是一个实现两者的实现:

    public class CustomOAuthBearerProvider : IOAuthBearerAuthenticationProvider
    {
        public Task ApplyChallenge(OAuthChallengeContext context)
        {
            context.Response.Redirect("/Account/Login");
            return Task.FromResult<object>(null);
        }
    
        public Task RequestToken(OAuthRequestTokenContext context)
        {
            string token = context.Request.Cookies[SessionKey];
            if (!string.IsNullOrEmpty(token))
            {
                context.Token = token;
            }
            return Task.FromResult<object>(null);
        }
        public Task ValidateIdentity(OAuthValidateIdentityContext context)
        {
            return Task.FromResult<object>(null);
        }
    }
    

    我不需要在ValidateIdentity中做任何特殊的事情,但我需要满足界面 .

    为了解决这个问题,请告诉您的应用使用JwtBearerAuthentication与您的提供商:

    // controllers with an [Authorize] attribute will be validated with JWT
    app.UseJwtBearerAuthentication(
        new JwtBearerAuthenticationOptions
        {
            AllowedAudiences = audiences.ToArray(),
            IssuerSecurityTokenProviders = providers.ToArray(),
            Provider = new CookieOAuthBearerProvider()
        }
    );
    

相关问题