积极答复者
身份验证 异常:No signing credential is configured by the 'IdentityServer:Key' configuration section

问题
-
在 Blazor server 项目中,
1、添加自定义 ApplicationUser 类,
public class ApplicationUser : IdentityUser { public string? CustomClaim { get; set; } }
2、添加自定义 ApplicationDbContext 类
public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser> { public ApplicationDbContext( DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions) { } }
3、添加自定义 AppClaimsPrincipalFactory 类
public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> { public AppClaimsPrincipalFactory( UserManager<ApplicationUser> userManager , RoleManager<IdentityRole> roleManager , IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor) { } protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user) { var identity = await base.GenerateClaimsAsync(user); if (!string.IsNullOrWhiteSpace(user.CustomClaim)) { identity.AddClaim(new Claim("custom_claim", user.CustomClaim)); } return identity; } }
4、 在 Startup 的文件中,添加:
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false) .AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddClaimsPrincipalFactory<AppClaimsPrincipalFactory>(); services.AddIdentityServer() .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => { options.IdentityResources["openid"].UserClaims.Add("role"); // Roles options.ApiResources.Single().UserClaims.Add("role"); options.IdentityResources["openid"].UserClaims.Add("custom_claim"); // Custom Claim options.ApiResources.Single().UserClaims.Add("custom_claim"); }); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("role"); services.AddAuthentication().AddIdentityServerJwt();
app.UseIdentityServer();
5、运行后,报错:
fail:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception occurred while processing the request.
InvalidOperationException: No signing credential is configured by the 'IdentityServer:Key' configuration section.
Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions+<>c.<AddSigningCredentials>b__10_2(IServiceProvider sp)6、IdentityServer:Key 这个key,需要在哪里配置?验证文件(*.pfx),要怎么创建?如果是部署在 IIS 中,要怎么存储 和 获取?
2021年9月13日 1:37
答案
全部回复
-
1、我用 OpenSSL ,创建了IS4.pfx 文件,复制到 Server\bin\Debug\net6.0 目录中
2、在 appsettings.json 中,修改:
"IdentityServer": { "Key": { "Type": "File", "FilePath": "IS4.pfx", "Password": "Aa888888" }, "Clients": { "MyApp.Client": { "Profile": "IdentityServerSPA" } }
3、 在Startup 文件中,修改:
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false) .AddRoles<IdentityRole>() // For Roles, see (client side) /RolesClaimsPrincipalFactory.cs .AddEntityFrameworkStores<ApplicationDbContext>() .AddClaimsPrincipalFactory<AppClaimsPrincipalFactory>(); // To add a custom claim for the user, see: /Models/ApplicationUser.cs string _pfx = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Configuration["IdentityServer:Key:FilePath"]); string _pw = Configuration["IdentityServer:Key:Password"]; services.AddIdentityServer() .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => { options.IdentityResources["openid"].UserClaims.Add("role"); // Roles options.ApiResources.Single().UserClaims.Add("role"); options.IdentityResources["openid"].UserClaims.Add("custom_claim"); // Custom Claim options.ApiResources.Single().UserClaims.Add("custom_claim"); }) .AddSigningCredential(new X509Certificate2(_pfx, _pw)); //.AddDeveloperSigningCredential(true,ConstanceHelper.AppSettings.CredentialFileName); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("role"); services.AddAuthentication().AddIdentityServerJwt();
运行后,出错:
找不到 localhost 的网页
找不到与以下网址对应的网页:https://localhost:5001/
HTTP ERROR 404- 已编辑 诺奇玛浓 2021年9月13日 4:16
2021年9月13日 4:14