asp.net - 如何使用 OWIN OAuthBearerAuthentication 验证访问 token ?

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

我想要什么:

  1. token 生成器使用 OAuthAuthorizationServer, token 使用者使用 OAuthBearerAuthentication(验证访问 token )。
  2. 使用 OWIN 管道管理所有内容、 token 内容和 Web API 内容。

代码怎么样:

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. 在 token 认证过程中,我想到的是解密访问 token ,验证来自客户端的 token ,如果验证了 token ,则将 token 的身份放入HttpContext.Current .用户.

    OAuthBearerAuthenticationProvider 的职责是履行 之前的步骤。我说得对吗?

最佳答案

如您所知,UseOAuthAuthorizationServer 负责对用户进行身份验证。然后,UseOAuthBearerAuthentication 的任务是确保只有经过身份验证的用户才能访问您的应用程序。通常,这两项工作被分配给不同的 Web 应用程序。看起来您的应用程序正在执行这两件事。

在某些情况下,您确实需要覆盖默认的OAuthBearerAuthenticationProvider。也许你这样做,也许你不这样做。在我的例子中,ApplicationCookie不太适合这个场景。因此,我将第 3 方 JWT token 存储在 cookie 中,而不是 header 中,并使用它来指示用户已通过 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 中做任何特殊的事情,但我需要满足接口(interface)。

要连接起来,请告诉您的应用程序将 JwtBearerAuthentication 与您的提供商一起使用:

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

关于asp.net - 如何使用 OWIN OAuthBearerAuthentication 验证访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097221/

相关文章:

asp.net-web-api - 带有 Web.API 2 的 MiniProfiler;是否有全局魔术请求上下文对象?

javascript - Knockout JS 绑定(bind)计算可观察值不起作用

asp.net - 如何跟踪 ASP.net 中的调试消息?

c# - 传入字典的模型项的类型为 'System.Net.Http.HttpResponseMessage' ,

azure - flutter 应用程序 : How to implement a proper logout function?

java - 如何使 Spring Security OAuth2 与负载均衡器一起工作?

java - ADAL 4 Android 不传递客户端 secret

asp.net - 为什么 ASP.NET 忽略我的成员(member)连接字符串?

c# - Bootstrap css 覆盖自定义 css 样式

.net - 如何使用 SignalR 将事件从 Web Api 项目广播到不同应用程序池上的 MVC 应用程序