【问题标题】:.Net Core 2.0 Web API using Identity / JWT and having user manager work with DI.Net Core 2.0 Web API 使用 Identity / JWT 并让用户管理器使用 DI
【发布时间】:2018-05-12 05:42:15
【问题描述】:

我也有同样的问题:

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

如果不添加:

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

您不能将用户管理器和登录管理器依赖注入到令牌控制器或其他控制器。 有什么想法可以解决这个问题吗?

【问题讨论】:

标签: jwt asp.net-identity-2 asp.net-core-webapi


【解决方案1】:

我将 AddDefaultTokenProviders() 添加到 services.AddIdentityCore 所以我的代码现在看起来像这样:

   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();

这对我有用。

现在可以关闭问题了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2021-11-24
    • 1970-01-01
    • 2018-05-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多