【发布时间】:2019-02-10 13:23:26
【问题描述】:
我正在将ASP.NET Web API 4.6 OWIN 应用程序移植到ASP.NET Core 2.1。该应用程序基于JWT 令牌工作。但是通过 cookie 而不是标头传递的令牌。我不确定为什么不使用标题,这只是我必须处理的情况。
考虑到身份验证不是通过 cookie 完成的。 cookie 仅用作传输媒体。在遗留应用程序中,CookieOAuthBearerProvider 用于从 cookie 中提取 JWT 令牌。配置代码如下:
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new[] { audienceId },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret)
},
Provider = new CookieOAuthBearerProvider("token")
});
}
CookieOAuthBearerProvider类源码源码如下:
public class CookieOAuthBearerProvider : OAuthBearerAuthenticationProvider
{
readonly string _name;
public CookieOAuthBearerProvider(string name)
{
_name = name;
}
public override Task RequestToken(OAuthRequestTokenContext context)
{
var value = context.Request.Cookies[_name];
if (!string.IsNullOrEmpty(value))
{
context.Token = value;
}
return Task.FromResult<object>(null);
}
此解决方案在here 进行了更详细的讨论。
现在我需要为 ASP.NET Core 实现类似的解决方案。问题是 UseJwtBearerAuthentication 不再存在于 ASP.NET Core 中,我不知道如何引入自定义 AuthenticationProvider。
非常感谢任何帮助。
更新: 有a solution that tries to validate JWT by its own code。这不是我需要的。我只是在寻找一种方法将从 cookie 收到的令牌传递给标头阅读器。
【问题讨论】:
标签: c# authentication cookies asp.net-core jwt