Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/google-chrome-extension/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/windows-phone-8/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.Net Core 2.0 Web API使用Identity/JWT并让用户经理与DI一起工作_Jwt_Asp.net Identity 2_Asp.net Core Webapi - Fatal编程技术网

.Net Core 2.0 Web API使用Identity/JWT并让用户经理与DI一起工作

.Net Core 2.0 Web API使用Identity/JWT并让用户经理与DI一起工作,jwt,asp.net-identity-2,asp.net-core-webapi,Jwt,Asp.net Identity 2,Asp.net Core Webapi,我的问题与此相同: 如果不添加: services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<IdentityDb>() .AddDefaultTokenProviders(); services.AddIdentity() .AddEntityFrameworkStores() .AddDefau

我的问题与此相同:

如果不添加:

            services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<IdentityDb>()
            .AddDefaultTokenProviders();
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
您不能将用户管理器和登录管理器注入令牌控制器或其他控制器。
你知道怎么解决吗?

我在services.AddIdentityCore中添加了AddDefaultTokenProviders() 现在我的代码如下所示:

   services.AddDbContext<IdentityDb>(options =>
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”))

IdentityBuilder builder=services.AddIdentityCore
(opt=>
{
opt.Password.RequireDigit=true;
opt.Password.RequiredLength=8;
opt.Password.RequireNonAlphanumeric=false;
opt.Password.RequireUppercase=true;
opt.Password.RequireLowercase=true;
}
).AddDefaultTokenProviders();
builder=newidentitybuilder(builder.UserType,typeof(IdentityRole),builder.Services);
建设者
.AddEntityFrameworkStores();
//.AddDefaultTokenProviders();
builder.AddRoleValidator();
builder.AddRoleManager();
builder.AddSignInManager();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(选项=>
{
options.TokenValidationParameters=新的TokenValidationParameters
{
validateisuer=true,
ValidateAudience=true,
ValidateLifetime=true,
ValidateSuersigningKey=true,
ValidIssuer=“WindowLink.Security.Bearer”,
validudience=“WindowLink.Security.Bearer”,
IssuerSigningKey=JwtSecurityKey.Create(“windowlink密钥”)
};
options.Events=newjwtbearerevents
{
OnAuthenticationFailed=上下文=>
{
Console.WriteLine(“OnAuthenticationFailed:+context.Exception.Message”);
返回Task.CompletedTask;
},
OnTokenValidated=上下文=>
{
Console.WriteLine(“OnTokenValidated:+context.SecurityToken”);
返回Task.CompletedTask;
}
};
});
services.AddAuthorization(选项=>
{
options.AddPolicy(“成员”,
policy=>policy.requirecall(“MembershipId”);
});
services.AddMvc();
这对我来说是个好办法

问题现在可以结束。

问题可能重复
        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();