根据此 Microsoft 文档,您应该能够将 [RequiredScope("SomeScopeName")] 等属性应用到控制器级别或操作级别以保护 API。但是当我在 API 中尝试它时,它似乎根本没有任何效果 - 无论我使用什么范围名称(我确保令牌中没有该名称的范围),我总是正确的进入我应该失败的 API 操作。但与此同时,我的策略属性,例如 [Authorize(Policy = "PolicyName")],工作得很好。我缺少什么?
[ApiController]
[RequiredScope("AnyRandomName")]
public class MyApiController : ControllerBase
{
Run Code Online (Sandbox Code Playgroud)
更新
这是我的 Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
IdentityModelEventSource.ShowPII = true;
services.AddControllers();
services.AddSwaggerGen(opt =>
{
opt.CustomSchemaIds(type => type.ToString() + type.GetHashCode());
});
services.Configure<HostOptions>(Configuration.GetSection(HostOptions.HOST));
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Authority = Configuration[HostOptions.IDENTITYGATEWAY];
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 HTTPS 连接我的 gRPC 客户端和服务,它们都在我的本地 Windows 10 计算机上的 .Net 5 下运行。现在我收到此证书错误,但不知道如何修复它:
Status(StatusCode=\"Internal\", Detail=\"Error starting gRPC call.
HttpRequestException: The SSL connection could not be established, see inner exception.
AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot\", DebugException=\"System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.\r\n
---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot\r\n at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)\r\n at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 ASP.Net Core 应用程序中使用 Serilog,但无法让它工作。一旦我在 Program.cs 中添加 .UseSerilog(),通常进入控制台屏幕的消息就会消失,并且不会创建任何日志文件。以下是我启用 Serilog 的方法:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序设置:
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Grpc": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
Run Code Online (Sandbox Code Playgroud)
据我所知,默认情况下,日志文件似乎会被写入 VS.net 项目下的“logs”文件夹中(至少从我查看的示例来看是这样)。我想念什么?
我在 HTTPS 下使用 IIS 服务器托管我的 IdentityServer、Web API 和 MVC 客户端应用程序。当我运行我的 MVC 客户端时,我收到以下错误(注意 - 6009 是我的 MVC 客户端的端口,6005 是我的 IdentityServer 的端口):
当我在 IIS 中配置它们时,我正在为所有项目使用 IIS Express 开发证书:
我将证书从个人商店复制到受信任的根目录,如下所示:
这是证书的详细信息:
我在这里还缺少什么?