C# “如何修复”;对象引用未设置为对象的实例";-调用RequestClientCredentialsTokenAsync时?
当我尝试将请求发送到:C# “如何修复”;对象引用未设置为对象的实例";-调用RequestClientCredentialsTokenAsync时?,c#,asp.net-core,identityserver4,C#,Asp.net Core,Identityserver4,当我尝试将请求发送到: var client = new HttpClient(); var resp = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = "http://localhost:5000/connect/token", ClientId = client
var client = new HttpClient();
var resp = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = "http://localhost:5000/connect/token",
ClientId = clientId.AsString(),
ClientSecret = clientSecret,
GrantType = grantType,
Scope = scopes
});
我正在捕获一个异常:System.NullReferenceException:“对象引用未设置为对象的实例:
crit: IdentityServer4.Hosting.IdentityServerMiddleware[0]
Unhandled exception: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at IdentityServer4.Validation.DefaultClientConfigurationValidator.ValidateUriSchemesAsync(ClientConfigurationValidationContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Validation\Default\DefaultClientConfigurationValidator.cs:line 148
at IdentityServer4.Validation.DefaultClientConfigurationValidator.ValidateAsync(ClientConfigurationValidationContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Validation\Default\DefaultClientConfigurationValidator.cs:line 52
at IdentityServer4.Stores.ValidatingClientStore`1.FindClientByIdAsync(String clientId) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Stores\ValidatingClientStore.cs:line 61
at IdentityServer4.Stores.IClientStoreExtensions.FindEnabledClientByIdAsync(IClientStore store, String clientId) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Extensions\IClientStoreExtensions.cs:line 23
at IdentityServer4.Validation.ClientSecretValidator.ValidateAsync(HttpContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Validation\Default\ClientSecretValidator.cs:line 67
at IdentityServer4.Endpoints.TokenEndpoint.ProcessTokenRequestAsync(HttpContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Endpoints\TokenEndpoint.cs:line 78
at IdentityServer4.Endpoints.TokenEndpoint.ProcessAsync(HttpContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Endpoints\TokenEndpoint.cs:line 70
at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Hosting\IdentityServerMiddleware.cs:line 54
这是一个关于错误的非常“好”的描述。。。我正在尝试使用resharper从库中调试标识代码。当我步进ValidatingClientStore.cs文件的第58行时,它由以下代码表示:
var context = new ClientConfigurationValidationContext(client)
我得到了上面提到的错误。它只是将我重定向到
C:\local\identity\server4\IdentityServer4\src\IdentityServer4\src\Validation\Default\DefaultClientConfigurationValidator.cs:line 148
对于方法签名。。。这就是全部。我不明白我做错了什么…我试图覆盖标识组件,但我被错误缠住了。
我的Startup.cs配置:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var scope = Configuration["Authentication:Scope:Name"];
services.AddCors(options =>
{
// this defines a CORS policy called "default"
options.AddPolicy("default", policy =>
{
policy.WithOrigins("http://localhost:4300")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddAuth(options =>
{
options.Security = new SecurityOptions
{
RequiredScope = scope,
IntrospectionOptions = new OAuth2IntrospectionOptions
{
Authority = Configuration["Authentication:Settings:Authority"],
ClientId = Configuration["Authentication:Settings:ApiName"],
ClientSecret = Configuration["Authentication:Settings:ApiSecret"],
SaveToken = bool.Parse(Configuration["Authentication:Settings:SaveToken"]),
NameClaimType = Configuration["Authentication:Settings:NameClaimType"],
RoleClaimType = Configuration["Authentication:Settings:RoleClaimType"]
}
};
});
services.Configure<List<ApiResourceOption>>(Configuration.GetSection("ApiResources"));
services.Configure<List<ClientOption>>(Configuration.GetSection("Clients"));
services.Configure<IdentityServerAuthenticationOptions>(Configuration.GetSection("Authentication:Settings"));
//register main stores for identity
services.AddIdentityCore<ApplicationUser>()
.AddRoles<ApplicationRole>()
.AddUserStore<ApplicationUserStore>()
.AddRoleStore<ApplicationRoleStore>()
.AddUserManager<ApplicationUserManager>()
.AddUserValidator<CustomUserValidator<ApplicationUser>>()
.AddDefaultTokenProviders();
//register identity server
services.AddIdentityServer(c =>
{
c.Caching.ClientStoreExpiration = TimeSpan.FromDays(1);
c.Caching.ResourceStoreExpiration = TimeSpan.FromDays(1);
})
.AddAspNetIdentity<ApplicationUser>()
.AddClientStore<ApplicationClientStore>()
.AddResourceStore<ApplicationResourceStore>()
.AddProfileService<ProfileService<ApplicationUser>>()
.AddResourceOwnerValidator<CustomResourceOwnerEmailValidator<ApplicationUser>>()
.AddDeveloperSigningCredential();
services.AddScoped<IPasswordHasher<ApplicationUser>, IdentityHasherService<ApplicationUser>>();
services.AddScoped<IPersistedGrantStore, ApplicationPersistedGrantStore>();
services.TryAddScoped<IPasswordValidator<ApplicationUser>, PasswordValidator<ApplicationUser>>();
services.TryAddScoped<IPasswordHasher<ApplicationUser>, PasswordHasher<ApplicationUser>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<ApplicationRole>, RoleValidator<ApplicationRole>>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<ApplicationUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimsPrincipalFactory<ApplicationUser, ApplicationRole>>();
services.TryAddScoped<UserManager<ApplicationUser>, UserManager<ApplicationUser>>();
services.TryAddScoped<SignInManager<ApplicationUser>, SignInManager<ApplicationUser>>();
services.TryAddScoped<RoleManager<ApplicationRole>, AspNetRoleManager<ApplicationRole>>();
services.Configure<AuthSettings>(Configuration.GetSection("Authentication:Settings"));
string connection = Configuration.GetConnectionString("Default");
services.AddDbContext<IdentityContext>(options =>
options.UseSqlServer(connection));
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddMvcCore().AddJsonFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("default");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseIdentityServer();
app.UseMvc();
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
var scope=Configuration[“身份验证:范围:名称”];
services.AddCors(选项=>
{
//这定义了一个名为“默认”的CORS策略
options.AddPolicy(“默认”,策略=>
{
政策来源(”http://localhost:4300")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddAuth(选项=>
{
options.Security=新的SecurityOptions
{
RequiredScope=范围,
内省选项=新的OAuth2Introspection选项
{
权限=配置[“身份验证:设置:权限”],
ClientId=配置[“身份验证:设置:ApiName”],
ClientSecret=Configuration[“身份验证:设置:ApiSecret”],
SaveToken=bool.Parse(配置[“身份验证:设置:SaveToken”]),
NameClaimType=配置[“身份验证:设置:NameClaimType”],
RoleClaimType=配置[“身份验证:设置:RoleClaimType”]
}
};
});
根据上面的堆栈跟踪配置,异常发生在ValidateUriSchemesAsync()中
,因此在其中一个客户端配置对象的重定向URI
或PostLogoutRedirecutURI
中很可能存在null
值。根据上面的堆栈跟踪,异常发生在ValidateUriSchemesAsync()中
,因此在其中一个客户端配置对象的重定向URI
或PostLogoutRedireDirectURI
中很可能存在null
值