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