我在最新的Web API项目中使用了基本的OAuth Bearer令牌stuf,一切都很好。我能够获得一个令牌,并将其传递回Web API并让它验证我并填充ClaimsPrincipal。然后,我可以获得API调用的授权,并使用ClaimsAuthorizeAttribute(来自Thinktecture.IdentityModel),并检查了我的web.config中的策略。
但是,我想尝试使用JWT而不是标准OAuth令牌,现在我无法使用它。它验证JWT就好了,它从验证代码返回ClaimsPrincipal,但它永远不会为当前用户设置,因此api上的授权代码失败,因为没有声明。
我更改了我的Startup.Auth.cs以包含以下内容,此代码与之前工作版本之间的唯一区别是我传递的是MyCustomJwtFormat()令牌格式类而不是原始版本:
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(5),
AllowInsecureHttp = false,
AccessTokenFormat = new MyCustomJwtFormat(audience, new X509CertificateSecurityTokenProvider(issuer, CertStoreStuff.GetCertificateFromStore("CN=XXXXXXX"))),
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active
};
MyCustomJwtFormat类实现ISecureDataFormat<AuthenticationTicket>
,unprotect()方法成功返回填充的ClaimsIdentity。
在使用JWT设置Principal时,是否需要采取一些手动步骤?