Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

JWT token validation

$
0
0

Hi,

I need to validate multiple token that I receive in same request, I followed below code flow

services.AddAuthentication()
        .AddJwtBearer("Token1", options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = true,
                ValidIssuer = Issuer,
                ValidateAudience = true,
                ValidAudience = Audience,
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Key)),
            };
            options.Events = new JwtBearerEvents()
            {
                OnMessageReceived = context =>
                {
                    var Token = context.Request.Headers["UserCred1"].ToString();
                    context.Token = Token;
                    return Task.CompletedTask;
                },
            };
        })
        .AddJwtBearer("Token2", options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = true,
                ValidIssuer = Issuer,
                ValidateAudience = true,
                ValidAudience = Audience,
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Key)),
            };
            options.Events = new JwtBearerEvents()
            {
                OnMessageReceived = context =>
                {
                    var Token = context.Request.Headers["UserCred2"].ToString();
                    context.Token = Token;
                    return Task.CompletedTask;
                },
            };
        });
    services.AddAuthorization(options =>
    {
        options.DefaultPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes("Token1", "Token2")
        .Build();
    });

Above code works as OR condition, like either Token1 or Token 2 authentication is valid then returns as Success. But for me I need to consider success only when both "Token1"AND, "Token2" are valid. 

Kindly let me know your thoughts..


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>