gpt4 book ai didi

c# - JWT Bearer Authentication 不从 header 中读取 token

转载 作者:行者123 更新时间:2023-11-30 21:30:23 28 4
gpt4 key购买 nike

我正在尝试使用 .Net-Core 中的 JWT Bearer 进行身份验证,这是我的启动:

var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));

// Configure JwtIssuerOptions
services.Configure<JwtIssuerOptions>(options =>
{
options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
});

var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],

ValidateAudience = true,
ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],

ValidateIssuerSigningKey = true,
IssuerSigningKey = _signingKey,

RequireExpirationTime = false,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;

cfg.Events = new JwtBearerEvents
{
OnMessageReceived = async (ctx) =>
{
Console.WriteLine(ctx.Token);
},

OnTokenValidated = async (ctx) =>
{
Console.WriteLine("BreakPoint");
},
};

cfg.TokenValidationParameters = tokenValidationParameters;
})
.AddCoinbase(options => {
options.AccessAllAccounts = true;
options.SendLimitAmount = 1;
options.SendLimitCurrency = "USD";
options.SendLimitPeriod = SendLimitPeriod.day;
options.ClientId = Configuration["Coinbase:ClientId"];
options.ClientSecret = Configuration["Coinbase:ClientSecret"];
COINBASE_SCOPES.ForEach(scope => options.Scope.Add(scope));
options.SaveTokens = true;
options.ClaimActions.MapJsonKey("urn:coinbase:avatar", "avatar_url");
});

我正在使用我的 access_token 从 Postman 发出一个简单的获取请求:

获取 https://localhost:44377/api/values HEADERS:授权:承载

然而,当我检查收到的消息上的 token 时我总是得到 null

OnMessageReceived = async (ctx) =>
{
Console.WriteLine(ctx.Token);
}

最佳答案

调用 OnMessageReceived 委托(delegate)时没有首先设置 Token 属性。对于此事件,Token 是您可以自己设置的内容,前提是您要覆盖 token 的检索方式。您可以在 source code 中亲自查看:

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
string token = null;
try
{
// Give application opportunity to find from a different location, adjust, or reject token
var messageReceivedContext = new MessageReceivedContext(Context, Scheme, Options);

// event can set the token
await Events.MessageReceived(messageReceivedContext);
if (messageReceivedContext.Result != null)
{
return messageReceivedContext.Result;
}

// If application retrieved token from somewhere else, use that.
token = messageReceivedContext.Token;

if (string.IsNullOrEmpty(token))
{
string authorization = Request.Headers["Authorization"];

...

Events.MessageReceived 的调用会调用您的 OnMessageReceived 委托(delegate),但是 MessageReceivedContext 尚未使用 Token< 进行初始化,所以它只是 null。在调用 Events.MessageReceived 之后,将从 Authorization header 中检索 token (如果您没有像我提到的那样自行设置的话)。

关于c# - JWT Bearer Authentication 不从 header 中读取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54496859/

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