相关疑难解决方法(0)

在ASP.NET Core中,从Cookie而不是标题中读取JWT令牌

我正在将ASP.NET Web API 4.6 OWIN应用程序移植到ASP.NET Core 2.1。该应用程序基于JWT令牌运行。但是令牌通过cookie而不是标头传递。我不确定为什么不使用标题,这只是我必须处理的情况。

考虑到身份验证不是通过cookie完成的。cookie仅用作传输介质。在旧版应用程序CookieOAuthBearerProvider中用于JWT从cookie中提取令牌。配置代码如下所示:

    app.UseJwtBearerAuthentication(
        new JwtBearerAuthenticationOptions
        {
            AuthenticationMode = AuthenticationMode.Active,
            AllowedAudiences = new[] { audienceId },
            IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
            {
                new SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret)
            },
            Provider = new CookieOAuthBearerProvider("token")
        });
}
Run Code Online (Sandbox Code Playgroud)

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 = …
Run Code Online (Sandbox Code Playgroud)

c# authentication cookies jwt asp.net-core

6
推荐指数
1
解决办法
1368
查看次数

标签 统计

asp.net-core ×1

authentication ×1

c# ×1

cookies ×1

jwt ×1