wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了.net – 如何用对称密钥配置MIcrosoft JWT?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试配置我的ASP.NET应用程序来接受使用对称密钥签名的JSON Web令牌(JWT)。 STS无法为此使用证书,因此我们使用对称密钥支持。 最后我使用了Microsoft’s JWT Developer Preview.不幸的是,我没有看到任何使用对称键的例子。在用各种工具进行一些挖掘之后,我发现了NamedKeyIssuerTokenResolver,发现我可以配置它使用对称密钥。例如
我正在尝试配置我的ASP.NET应用程序来接受使用对称密钥签名的JSON Web令牌(JWT)。 STS无法为此使用证书,因此我们使用对称密钥支持

最后我使用了Microsoft’s JWT Developer Preview.不幸的是,我没有看到任何使用对称键的例子。在用各种工具进行一些挖掘之后,我发现了NamedKeyIssuerTokenResolver,发现我可以配置它使用对称密钥。例如:

<securityTokenHandlers>
  <add type="Microsoft.Identitymodel.Tokens.JWT.JWTSecurityTokenHandler,Microsoft.Identitymodel.Tokens.JWT" />
  <securityTokenHandlerConfiguration>
    <certificateValidation certificateValidationMode="PeerTrust" />
    <issuerTokenResolver
      type="Microsoft.Identitymodel.Tokens.JWT.NamedKeyIssuerTokenResolver,Microsoft.Identitymodel.Tokens.JWT">
      <securityKey
          symmetricKey="+zqf97FD/xyzzyplugh42ploverFeeFieFoeFooxqje="
             name="https://localhost/TestRelyingParty" />
    </issuerTokenResolver>
  </securityTokenHandlerConfiguration>
</securityTokenHandlers>

我不完全确定我应该在那里使用什么名字。应该是观众Uri,也许是发行人Uri?在任何情况下,我知道如果我不包含一个名字,我的程序启动时会得到异常,因为SecurityKey元素需要该属性

无论如何,这仍然无法解决问题。在对STS进行身份验证后,我得到以下异常:

[SecurityTokenValidationException: JWT10310: Unable to validate signature. validationParameters.SigningTokenResolver type: 'Microsoft.Identitymodel.Tokens.JWT.NamedKeyIssuerTokenResolver',was unable to resolve key to a token.
The SecurityKeyIdentifier is: 
'SecurityKeyIdentifier
    (
    IsReadOnly = false,Count = 1,Clause[0] = Microsoft.Identitymodel.Tokens.JWT.NamedKeyIdentifierClause
    )
'. validationParameters.SigningToken was null.]
   Microsoft.Identitymodel.Tokens.JWT.JWTSecurityTokenHandler.ValidateSignature(JWTSecurityToken jwt,TokenValidationParameters validationParameters) +2111
   Microsoft.Identitymodel.Tokens.JWT.JWTSecurityTokenHandler.ValidateToken(JWTSecurityToken jwt,TokenValidationParameters validationParameters) +138
   Microsoft.Identitymodel.Tokens.JWT.JWTSecurityTokenHandler.ValidateToken(SecurityToken token) +599
   System.Identitymodel.Tokens.SecurityTokenHandlerCollection.ValidateToken(SecurityToken token) +135
   System.Identitymodel.services.TokenReceiver.AuthenticateToken(SecurityToken token,Boolean ensureBearerToken,String endpointUri) +117
   System.Identitymodel.services.WSFederationAuthenticationModule.SignInWithResponsemessage(httprequestBase request) +698
   System.Identitymodel.services.WSFederationAuthenticationModule.onAuthenticaterequest(Object sender,EventArgs args) +123924
   System.Web.SyncEventEXECUTIONStep.System.Web.httpApplication.IEXECUTIONStep.Execute() +80
   System.Web.httpApplication.ExecuteStep(IEXECUTIONStep step,Boolean& completedSynchronously) +165

我是否缺少其他配置步骤?我把错误的东西放在名称属性中吗?还是JWT开发者预览中的一个已知错误

2014/02/13更新:

正如@leastprivilege所指出的那样,使用RTM版本的JWT来说,这是一个很容易的事情。我强烈建议你忽略这个,并按照他在http://leastprivilege.com/2013/07/16/identityserver-using-ws-federation-with-jwt-tokens-and-symmetric-signatures/提供的例子。

请注意,以下原始答案是测试版本Microsoft.Identitymodel.Tokens.JWT。升级到发行版本System.Identitymodel.Tokens.Jwt,只需要一点工作。见下文。

主要的问题是JWTSecurityTokenHandler.ValidateToken(token)的方法并未完全填充它传递给JWTSecurityTokenHandler.ValidateToken(token,validationParameters)的TokenValidationParameters。特别是,它不填充SigningToken成员或ValidIssuers(或ValidIssuer)。

有趣的是,我在原来的问题中显示的配置实际上由令牌解析器加载,并且在运行时可用,如下面代码所示。

但是,我不知道如何在配置文件中指定有效的issuer字符串。我强烈怀疑有一个地方放这个信息,但我还没有弄清楚它在哪里。

解决我的问题是创建一个派生自JWTSecurityTokenHandler的自定义安全令牌处理程序。覆盖ValidateToken(token,validationParameters)使我有机会设置我需要的参数,然后调用基类的ValidateToken方法

public class CustomJwtSecurityTokenHandler: JWTSecurityTokenHandler
{
    // Override ValidateSignature so that it gets the SigningToken from the configuration if it doesn't exist in
    // the validationParameters object.
    private const String KeyName = "https://localhost/TestRelyingParty";
    private const String ValidIssuerString = "https://mySTSname/trust";
    public override ClaimsPrincipal ValidateToken(JWTSecurityToken jwt,TokenValidationParameters validationParameters)
    {
        // set up valid issuers
        if ((validationParameters.ValidIssuer == null) &&
            (validationParameters.ValidIssuers == null || !validationParameters.ValidIssuers.Any()))
        {
            validationParameters.ValidIssuers = new List<String> {ValidIssuerString};
        }
        // and signing token.
        if (validationParameters.SigningToken == null)
        {
            var resolver = (NamedKeyIssuerTokenResolver)this.Configuration.IssuerTokenResolver;
            if (resolver.SecurityKeys != null)
            {
                List<SecurityKey> skeys;
                if (resolver.SecurityKeys.TryGetValue(KeyName,out skeys))
                {
                    var tok = new NamedKeySecurityToken(KeyName,skeys);
                    validationParameters.SigningToken = tok;
                }
            }
        }
        return base.ValidateToken(jwt,validationParameters);
    }
}

在我的Web.config中,我只需要更改安全令牌处理程序:

<securityTokenHandlers>
    <!--<add type="Microsoft.Identitymodel.Tokens.JWT.JWTSecurityTokenHandler,Microsoft.Identitymodel.Tokens.JWT" />-->
    <!-- replaces the default JWTSecurityTokenHandler -->
    <add type="TestRelyingParty.CustomJwtSecurityTokenHandler,TestRelyingParty" />

没有什么可以花三,四天的时间来研究用几十行代码解决的问题。 。 。

新增版本

2013年6月,微软正式发布了其JWT。他们将命名空间更改为System.Identitymodel.Tokens.Jwt。升级后,上述解决方案停止工作。为了使它工作,我不得不@L_618_75@以下到我的CustomJwtSecurityTokenHandler。这是现有的代码

public override ClaimsPrincipal ValidateToken(JwtSecurityToken jwt)
{
    var vparms = new TokenValidationParameters
        {
            AllowedAudiences = Configuration.AudienceReStriction.AllowedAudienceUris.SELEct(s => s.ToString())
        };
    return ValidateToken(jwt,vparms);
}

大佬总结

以上是大佬教程为你收集整理的.net – 如何用对称密钥配置MIcrosoft JWT?全部内容,希望文章能够帮你解决.net – 如何用对称密钥配置MIcrosoft JWT?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。