1

I have the same problem as this:

.Net Core 2.0 Web API using JWT - Adding Identity breaks the JWT authentication

If you don't add:

            services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<IdentityDb>()
            .AddDefaultTokenProviders();

You can't Dependency inject the user manager, and sign in manager, to the Token Controller or other controllers. Any ideas how you fix that?

Gatekeeper
  • 29
  • 2
  • Possible duplicate of [.Net Core 2.0 Web API using JWT - Adding Identity breaks the JWT authentication](https://stackoverflow.com/questions/46323844/net-core-2-0-web-api-using-jwt-adding-identity-breaks-the-jwt-authentication) – Wagner Danda da Silva Filho Nov 28 '17 at 18:13

1 Answers1

-1

I added AddDefaultTokenProviders() to services.AddIdentityCore so my code now looks like this:

   services.AddDbContext<IdentityDb>(options =>

options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        IdentityBuilder builder = services.AddIdentityCore<ApplicationUser>
        (opt =>
        {
            opt.Password.RequireDigit = true;
            opt.Password.RequiredLength = 8;
            opt.Password.RequireNonAlphanumeric = false;
            opt.Password.RequireUppercase = true;
            opt.Password.RequireLowercase = true;
        }
        ).AddDefaultTokenProviders();
        builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
        builder
            .AddEntityFrameworkStores<IdentityDb>();
        //.AddDefaultTokenProviders();

        builder.AddRoleValidator<RoleValidator<IdentityRole>>();
        builder.AddRoleManager<RoleManager<IdentityRole>>();
        builder.AddSignInManager<SignInManager<ApplicationUser>>();

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,

                        ValidIssuer = "WindowLink.Security.Bearer",
                        ValidAudience = "WindowLink.Security.Bearer",
                        IssuerSigningKey = JwtSecurityKey.Create("windowlink-secret-key")
                    };

                    options.Events = new JwtBearerEvents
                    {
                        OnAuthenticationFailed = context =>
                        {
                            Console.WriteLine("OnAuthenticationFailed: " + context.Exception.Message);
                            return Task.CompletedTask;
                        },
                        OnTokenValidated = context =>
                        {
                            Console.WriteLine("OnTokenValidated: " + context.SecurityToken);
                            return Task.CompletedTask;
                        }
                    };
                });

        services.AddAuthorization(options =>
        {
            options.AddPolicy("Member",
                policy => policy.RequireClaim("MembershipId"));
        });

        services.AddMvc();

That did the trick for me.

Question can now be closed.

Gatekeeper
  • 29
  • 2