我正在使用可操作的消息(通过Outlook Web App)来调用Logic App。因此,我在请求中得到了Bearer令牌:
“动作授权”:“ Bearer eyJ0eXAi ...”
Callstack:
Outlook web app -> Logic App -> my endpoint hosted in azure
现在,我尝试使用jwt.io验证令牌,但出现签名无效的问题。
因此,我尝试使用JwtSecurityTokenHandler
在c#中对其进行验证。
我试图将https://substrate.office.com/sts/
添加到发行者列表中,但似乎验证甚至没有到达那里。
我正在使用以下代码来验证office.com发行的jwt令牌:
bool IsAuthorized(HttpActionContext actionContext)
{
var valid = base.IsAuthorized(actionContext);
// Custom handle for Bearer token, when invalid from base-class
if (!valid && actionContext.Request.Headers.Authorization.Scheme == "Bearer")
{
var jwt = actionContext.Request.Headers.Authorization.Parameter;
var th = new JwtSecurityTokenHandler();
var sjwt = th.ReadToken(jwt) as JwtSecurityToken;
var validationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = false,
//IssuerSigningToken = sjwt,
ValidateActor = false,
ValidateAudience = false,
ValidateIssuer = true,
ValidateLifetime = true,
ValidIssuers = new[] { "https://substrate.office.com/sts/" },
ValidAudiences = new[] {"https://XXX.logic.azure.com"}
};
SecurityToken validatedToken;
try
{
th.ValidateToken(jwt, validationParameters, out validatedToken);
}
catch (Exception ex)
{
return false;
}
}
return valid;
}
这是我的JWT令牌:
我遇到了例外:
IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier
(
IsReadOnly = False,
Count = 2,
Clause[0] = X509ThumbprintKeyIdentifierClause(Hash = 0x818...),
Clause[1] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause
)
', ...
即使我设置了ValidateIssuerSigningKey = false
。
是否可以接受https://substrate.office.com/sts/
作为有效发行人?
答案 0 :(得分:0)
该异常表明“签名验证失败”。
要解决此问题,我们不能仅将所需的有效发行者添加到ValidIssuers
,我们需要通过验证来验证令牌是由发行者本身发行的。
特别是在这种情况下,office.com是发行者,我在这里找到了预期的密钥(JWK-JSON Web密钥): https://substrate.office.com/sts/common/discovery/keys (也是https://substrate.office.com/sts/common/.well-known/openid-configuration)
这是工作代码:
bool IsAuthorized(HttpActionContext actionContext)
{
var valid = base.IsAuthorized(actionContext);
// Custom handle for Bearer token, when invalid from base-class
if (!valid && actionContext.Request.Headers.Authorization.Scheme == "Bearer")
{
var jwt = actionContext.Request.Headers.Authorization.Parameter;
var th = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new JsonWebKey(GetJWK()),
ValidIssuers = new[] { "https://substrate.office.com/sts/" }
};
Microsoft.IdentityModel.Tokens.SecurityToken validatedToken;
try
{
var claims = th.ValidateToken(jwt, validationParameters, out validatedToken);
valid = true;
}
catch (Exception ex)
{
valid = false;
}
}
return valid;
}
// Get the token from configuration
private string GetJWK()
{
return ConfigurationManager.AppSettings["ida:jwks_json"];
}
在appsettings中,我从网站上放了RSA密钥来验证令牌,它看起来像:
{"kty":"RSA","use":"sig","kid":"gY...","x5t":"gY...","n":"2w...","e":"AQAB","x5c":["MII..."]}