gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:58:43 25 4
gpt4 key购买 nike

我想要什么:

  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com