配置ASP.NET MVC 4以使用Azure的ACS和Microsoft的新System.IdentityModel.Tokens.Jwt库

时间:2013-06-12 01:42:26

标签: asp.net-mvc-4 jwt azure-acs

如何将新的JWT处理程序库(System.IdentityModel.Tokens.Jwt)的1.0.0版集成到ASP.NET MVC 4应用程序中以处理来自ACS的Azure的JWT令牌?

我尝试运行应用程序时收到以下错误:

  

[SecurityTokenValidationException:Jwt10329:无法验证   签名,Configuration.IssuerTokenResolver.ResolveToken返回   空值。 jwt.Header.SigningKeyIdentifier:' SecurityKeyIdentifier       (       IsReadOnly = False,       数= 2,       条款[0] = X509ThumbprintKeyIdentifierClause(Hash = 0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX),       Clause [1] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause       )'。] System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(JwtSecurityToken   jwt)+1275
  System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(JwtSecurityToken   jwt)+113
  System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(SecurityToken   令牌)+339
  System.IdentityModel.Tokens.SecurityTokenHandlerCollection.ValidateToken(SecurityToken   令牌)+73
  System.IdentityModel.Services.TokenReceiver.AuthenticateToken(SecurityToken   token,Boolean ensureBearerToken,String endpointUri)+120
  System.IdentityModel.Services.WSFederationAuthenticationModule.SignInWithResponseMessage(HttpRequestBase   请求)+493
  System.IdentityModel.Services.WSFederationAuthenticationModule.OnAuthenticateRequest(对象   发件人,EventArgs args)+364
  System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)+69

我的web.config配置如下:

<system.identityModel>

    <identityConfiguration>
      <audienceUris>
        <add value="http://127.0.0.1:81/" />
      </audienceUris>

      <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <trustedIssuers>
                  <add thumbprint="PRIVATEKEY"
                     name="https://CUSTOM.accesscontrol.windows.net/" />
        </trustedIssuers>
      </issuerNameRegistry>

      <securityTokenHandlers>
        <add type="System.IdentityModel.Tokens.JwtSecurityTokenHandler, System.IdentityModel.Tokens.Jwt" />
        <securityTokenHandlerConfiguration>
          <certificateValidation certificateValidationMode="PeerTrust" />
        </securityTokenHandlerConfiguration>
        <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </securityTokenHandlers>

    </identityConfiguration>

  </system.identityModel>

  <system.identityModel.services>
    <federationConfiguration>
      <cookieHandler requireSsl="false" />
      <wsFederation passiveRedirectEnabled="false" issuer="https://staging.accesscontrol.windows.net/v2/wsfederation" realm="http://127.0.0.1:81/" requireHttps="false" />
    </federationConfiguration>

  </system.identityModel.services>

我已经设置了Azure ACS以返回JWT令牌,并在web.config中设置了正确的安全缩略图,但我很难理解这个错误发生的原因。任何见解?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。使用JWT,Web应用程序需要了解有关颁发者的信息才能验证令牌。 JWT缺少X509证书,需要在证书库中提供。 Vittorio B.在“在WIF应用程序中使用JWT处理程序”一节中描述了问题及解决问题的步骤here

答案 1 :(得分:0)

我能够通过创建新的x.509证书并将其作为主要X.509证书上载到Azure ACS然后将其安装在我本地计算机的凭据存储上来解决此问题。

我按照这些说明创建了证书:

http://blogs.msdn.com/b/cclayton/archive/2012/03/21/windows-azure-and-x509-certificates.aspx

我使用makecert命令生成证书(确保添加自己的命名空间)

makecert.exe -r -pe -a sha1 -n "CN=YOURNAMESPACE.accesscontrol.windows.net" -ss My -sr CurrentUser -len 2048 -sky exchange -sy 24

然后我使用certmgr.mcs将证书导出为PFX和CER文件。

我将PFX文件导入Azure ACS(使用管理门户)。完成此操作后,我复制了新缩略图并将其粘贴到我的web.config文件中的旧值

最后,我将CER文件安装到我的证书库中,如本博文中所述:

http://www.cloudidentity.com/blog/2012/11/20/introducing-the-developer-preview-of-the-json-web-token-handler-for-the-microsoft-net-framework-4-5-2/

上述博客文章中的相关文字如下:

.CER。双击该文件,点击“安装证书...”按钮,选择“本地计算机”,“受信任的人”,然后您就可以开展业务了。

现在一切正常。希望这也适合你。如果您需要更多帮助,请询问,我将尝试指出您正确的方向

相关问题