IssuerSigningKeyResolver Error Cannot Implicitly Convert Type

156 views Asked by At

I'm doing a course on Udemy

Build an app with ASPNET Core and Angular from scratch

At the end of Section 4 I'm getting this error message:

Cannot implicitly convert type 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey' to 'Microsoft.IdentityModel.Tokens.IssuerSigningKeyResolver' [API]

My code is identical to the 'Instructors' :

using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;

namespace API.Extensions
{
    public static class IdentityServiceExtensions
    {
        public static IServiceCollection AddIdentityService(this IServiceCollection services, IConfiguration config)
        {
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKeyResolver = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["TokenKey"])),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });

            return services;
        }
    }
}

When I hover over the error it says:
Cannot implicitly convert type 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey' to 'Microsoft.IdentityModel.Tokens.IssuerSigningKeyResolver' [API]csharp(CS0029)

Tbh, I have no idea what I'm doing wrong here, there are no quick fixes available, and I don't think I'm trying to convert anything? I've literally just been following along with the instructor's code and Google isn't returning much. Thanks in advance.

1

There are 1 answers

1
AlexD4K On

IssuerSigningKeyResolver is a delegade that retrieves the SecurityKey for signature validation and not a security key itself. When you're using multiple SecurityKeys it can be helpful if the SecurityToken does not contain a key identifier. But since you're probably only using one, its sufficient to only set the IssuerSigningKey and remove the resolver.