gpt4 book ai didi

c# - Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 警告 : 0 : invalid bearer token received

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:14 26 4
gpt4 key购买 nike

每当用户尝试访问 protected api 端点时,我都会收到此警告。身份验证工作正常,用户似乎已通过身份验证,但我一直无法弄清楚为什么此错误不断发生。我在没有启用 https/ssl 的 localhost 开发服务器上运行它。我可能错过了一个不太确定的步骤。我使用自定义提供程序实现了 token 身份验证和刷新 token 。然后实现自定义 jwt 格式来生成 token 。

This is the warning in the Application Output Log:

Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware Warning: 0 : invalid bearer token received

SimpleJWTFormat.cs:指定 jwt token 的格式

public class SimpleJwtFormat : ISecureDataFormat<AuthenticationTicket>
{

private readonly string _issuer = string.Empty;

public SimpleJwtFormat(string issuer)
{
_issuer = issuer;
}

public string Protect(AuthenticationTicket data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}

string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];

string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);

//var signingKey = new HmacSigningCredentials(keyByteArray);
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(keyByteArray);
securityKey.KeyId = ConfigurationManager.AppSettings["as:AudienceId"];

var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

var issued = data.Properties.IssuedUtc;

var expires = data.Properties.ExpiresUtc;

var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingCredentials);
//token.SigningKey = securityKey;

var handler = new JwtSecurityTokenHandler();

var jwt = handler.WriteToken(token);
return jwt;
}

public AuthenticationTicket Unprotect(string protectedText)
{
string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];

var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);
var signingKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(keyByteArray);

var tokenValidationParameters = new TokenValidationParameters
{
ValidAudience = audienceId,
ValidIssuer = _issuer,
IssuerSigningKey = signingKey,
ValidateLifetime = true,
ValidateAudience = true,
ValidateIssuer = true,
RequireSignedTokens = true,
RequireExpirationTime = true,
ValidateIssuerSigningKey = true
};


var handler = new JwtSecurityTokenHandler();
SecurityToken token = null;

// Unpack token
var pt = handler.ReadToken(protectedText);
string t = ((JwtSecurityToken)pt).RawData;

var principal = handler.ValidateToken(t, tokenValidationParameters, out token);
var identity = principal.Identities;

return new AuthenticationTicket(identity.First(), new AuthenticationProperties());
}
}

最佳答案

问题是我没有在 Startup.cs 文件中调用 app.UseOAuthBearerAuthentication 并指定自定义 token 格式。

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
AccessTokenFormat = _tokenFormat
});

关于c# - Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 警告 : 0 : invalid bearer token received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54826249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com