我想驗證使用oAuth 2.0中間件的智威湯遜。我想在我的Startup.cs類使用自定義提供者:如何在oAuth 2.0/owin中自定義JWT令牌驗證?
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
// Web API routes
config.MapHttpAttributeRoutes();
ConfigureOAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
//For Dev enviroment only (on production should be AllowInsecureHttp = false)
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/oauth2/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(5),
Provider = new CustomOAuthProvider(),
AccessTokenFormat = new RMAJwtAuthenticator.CustomJwtFormat("www.abc.com")
};
// OAuth 2.0 Bearer Access Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
// start : Code for Validating JWT
var issuer = "www.abc.com";
var audience = "www.xyz.com";
var secret = TextEncodings.Base64Url.Decode("Yuer534553HDS&dsa");
// Api controllers with an [Authorize] attribute will be validated with JWT
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
},
Provider = new CustomOAuthBearerProvider()
});
//End: Code for Validating JWT
}
}
在我CustomOAuthBearerProvider它繼承IOAuthBearerAuthenticationProvider,我提供ApplyChallenge(),RequestToken()和ValidateIdentity()的認定中:
public class CustomOAuthBearerProvider : IOAuthBearerAuthenticationProvider
{
public Task ApplyChallenge(OAuthChallengeContext context)
{
return Task.FromResult<object>(null);
}
public Task RequestToken(OAuthRequestTokenContext context)
{
return Task.FromResult<object>(null);
}
public Task ValidateIdentity(OAuthValidateIdentityContext context)
{
return Task.FromResult<object>(null);
}
}
現在,當我試圖獲得一個授權資源時,第一個RequestToken()被擊中,然後我不知道JWT如何驗證並且控件被傳遞給ValidateIdentity()方法。
我想定製驗證過程的原因是保存並延長我的JWT在數據庫中的過期時間(您也可以建議任何可以在不更改原始令牌的情況下增加JWT過期時間的任何內容)。
請評論,無論你的想法/建議/良好的不良練習選項/鏈接你會覺得有幫助。 謝謝。