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

时间:2014-08-02 16:40:10

标签: asp.net asp.net-web-api oauth-2.0 owin

我想要的:

  1. 令牌生成器使用OAuthAuthorizationServer和令牌使用者使用OAuthBearerAuthentication(验证访问令牌)。
  2. 使用OWIN管道来管理所有内容,令牌内容和web api内容。
  3. 代码怎么样:

    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());
    }
    

    有什么问题:

    1. OAuthBearerAuthenticationProvider的3个属性, ApplyChallengeRequestTokenValidateIdentity。如何 实施3方法?

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

      OAuthBearerAuthenticationProvider的责任是履行。{1}} 以前的步骤。我是对的吗?

1 个答案:

答案 0 :(得分:6)

如您所知,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()
    }
);