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:
-
OAuthBearerAuthenticationProvider
,ApplyChallenge
,_28863382_和ValidateIdentity
的3个属性 . 如何实现3方法? -
在令牌认证过程中,我想到的是解密访问令牌,验证来自客户端的令牌,如果令牌已经过验证,则将令牌的身份放入
HttpContext.Current.User
.
OAuthBearerAuthenticationProvider
的责任是完成前面的步骤 . 我对吗?
1 回答
如您所知,UseOAuthAuthorizationServer具有验证用户身份的工作 . 然后,UseOAuthBearerAuthentication的工作是确保只有经过身份验证的用户才能访问您的应用程序 . 通常,这两个作业被分配给不同的Web应用程序 . 看起来你的应用程序正在做这两件事 .
当然有些情况下您需要覆盖默认的OAuthBearerAuthenticationProvider . 也许你这样做,或者你可能没有 . 在我的情况下,ApplicationCookie并不适合这种情况 . 因此,我将第三方JWT令牌存储在cookie中,而不是标头中,并使用它来指示用户已通过Web应用程序的身份验证 . 我还需要重定向到我自己的登录页面,而不是提供401 .
这是一个实现两者的实现:
我不需要在ValidateIdentity中做任何特殊的事情,但我需要满足界面 .
为了解决这个问题,请告诉您的应用使用JwtBearerAuthentication与您的提供商: