簡體   English   中英

ASP.Net 5-OAuth承載令牌(JWT)驗證錯誤

[英]ASP.Net 5 - OAuth bearer token (JWT) validation error

我一直在研究使用JSON Web令牌對我的網站上的用戶進行身份驗證和授權的系統。 我的系統即將完成,但是當我嘗試在代碼中使用[Authorize(“ Bearer”)]屬性時,我遇到了錯誤。 錯誤如下:

發生System.IdentityModel.Tokens.SecurityTokenInvalidSignatureException消息:拋出異常:Microsoft.IdentityModel.Logging.dll中的“ System.IdentityModel.Tokens.SecurityTokenInvalidSignatureException”其他信息:IDX10503:簽名驗證失敗。 嘗試的鍵:“。 捕獲到異常:“。 令牌:'{“ typ”:“ JWT”,“ alg”:“ RS256”,“ kid”:null}。{“ nameid”:“ 6581f5a0-1775-4ce4-8650-a3d7e613b216”,“ unique_name”:“ alex “,” AspNet.Identity.SecurityStamp“:” 8da933c3-0f88-42ea-876d-c07e99d1eecc“,” iss“:” Uniti“,” aud“:” Uniti“,” exp“:1436849284,” nbf“:1436845684} '

我不明白為什么它不使用JWT測試任何密鑰。 我在啟動文件中定義了RSA密鑰。 在不加贅述的情況下,我在下面提供了解決此錯誤所需的代碼。

我的啟動代碼(生成密鑰和OAuthBearer選項):

#region RSA Key Generation

        var rsa = new RSACryptoServiceProvider(2048);
        var rsaKey = rsa.ExportParameters(true);
        var key = new RsaSecurityKey(rsaKey);

        services.AddInstance(new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest));

        #endregion

        services.AddInstance(new OAuthBearerAuthenticationOptions
        {
            SecurityTokenValidators = new List<ISecurityTokenValidator>
            {
                new JwtSecurityTokenHandler()
            },
            TokenValidationParameters = new TokenValidationParameters
            {
                IssuerSigningKey = key,
                ValidIssuer = "Uniti",
                ValidAudience = "Uniti"
            },
        });

        services.AddAuthorization();
        services.ConfigureAuthorization(auth =>
        {
            auth.AddPolicy("Bearer", builder =>
            {
                builder.AddAuthenticationSchemes(OAuthBearerAuthenticationDefaults.AuthenticationScheme);
                builder.RequireAuthenticatedUser();
            });
        });

我的令牌生成代碼:

var claimsIdentity = (ClaimsIdentity) User.Identity;

            var handler = BearerOptions.SecurityTokenValidators.OfType<JwtSecurityTokenHandler>().First();
            var securityToken = handler.CreateToken(
                issuer: "Uniti",
                audience: "Uniti",
                signingCredentials: BearerCredentials,
                subject: claimsIdentity
                );

            var token = handler.WriteToken(securityToken);

我是否忘記在某處添加內容,還是我生成的密鑰不正確? 提前謝謝您能為我提供幫助!

我敢打賭,這是由於注冊OAuth2承載選項的方式不正確 ,正如我先前的回答中已經解釋的那樣: https : //stackoverflow.com/a/31322654/542757

services.AddInstance(new OAuthBearerAuthenticationOptions());

當您使用services.AddInstance ,OAuth2承載中間件無法檢索選項(因而也檢索到密鑰),因為它在內部使用IOptions<OAuthBearerAuthenticationOptions>而不是OAuthBearerAuthenticationOptions

這是注冊OAuth2承載選項的正確方法:

services.ConfigureOAuthBearerAuthentication(options => {
    // Configure the options used by the OAuth2 bearer middleware.
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM