【问题标题】:remote certificate is invalid according to the validation procedure (Identity server Hosted in azure)根据验证程序,远程证书无效(身份服务器托管在 azure 中)
【发布时间】:2021-06-14 13:56:25
【问题描述】:

我尝试了很多,现在把它放在这里..所以我有一个应用服务,其中 API 身份服务器和 UI(Blazor)托管在同一个应用服务内的不同文件夹中,现在我生成了 rsa 签名证书 [https://damienbod.com/2020/02/10/create-certificates-for-identityserver4-signing-using-net-core/](这篇博文) 现在,即使我将托管身份服务器设置为提供者和 localhost(UI 和 Web API),即使它在工作 但是当我尝试访问托管 API 时,它的抛出错误(在日志中),我得到 401

如有任何帮助,将不胜感激

还有

我的身份服务器启动看起来像这样

public void ConfigureServices(IServiceCollection services)
    {
        var settings = config.GetSection("AppSettings").Get<AppSettings>();
        X509Certificate2 rsaCertificate = null;
        if (env.IsDevelopment())
        {
            rsaCertificate = new X509Certificate2(
            Path.Combine(env.WebRootPath, "cert/rsaCert.pfx"), "password");
        }
        else
        {
            using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                certStore.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = certStore.Certificates.Find(
                    X509FindType.FindByThumbprint,
                    settings.CertificateDetails.CertificateThumbPrint,
                    false);
                // Get the first cert with the thumbprint
                if (certCollection.Count > 0)
                {
                    rsaCertificate = certCollection[0];
                }
            }
        }
        var connectionString = config.GetConnectionString("DefaultConnection");
        services.AddOidcStateDataFormatterCache();
        services.AddDbContext<SeatingDBContext>(x =>
        {
            x.UseSqlServer(connectionString);
        });

        services.AddIdentity<ApplicationUser, ApplicationRole>(x =>
        {
            x.Password.RequiredLength = 4;
            x.Password.RequireDigit = false;
            x.Password.RequireNonAlphanumeric = false;
            x.Password.RequireUppercase = false;
        }).AddEntityFrameworkStores<SeatingDBContext>().
     AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(x =>
        {
            x.Cookie.Name = "IdentityServer.Cookie";
            x.LoginPath = "/Auth/Login";
        });

        var assembly = typeof(Startup).Assembly.GetName().Name;
        services.AddIdentityServer(x =>
        {
            x.Events.RaiseErrorEvents = true;
            x.Events.RaiseFailureEvents = true;
            x.Events.RaiseSuccessEvents = true;
            x.Events.RaiseInformationEvents = true;
        }).AddAspNetIdentity<ApplicationUser>()
  .AddInMemoryIdentityResources(Configuration.GetIdentityResources())
  .AddInMemoryApiScopes(Configuration.GetApiScopes())
   .AddInMemoryApiResources(Configuration.GetApis())
   .AddInMemoryClients(Configuration.GetClients(settings.ClientApps))
   .AddSigningCredential(rsaCertificate);
// .AddValidationKey(rsaCertificate);

        services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));

        services.AddTransient<IUserSyncService, UserSyncService>();

        services.AddControllersWithViews();
    }

我的 Web API 看起来像这样

public void ConfigureServices(IServiceCollection services)
    {
        var connectionString = Configuration.GetConnectionString("DefaultConnection");
        var settings = Configuration.GetSection("AppSettings").Get<AppSettings>();
        services.AddControllers();
        services.AddDbContext<SeatingDBContext>(x => x.UseSqlServer(connectionString));
        services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
        services.AddAutoMapper(typeof(Startup));
        services.AddScoped<IEmailService, EmailService>();
        services.AddSingleton<IEmailConfiguration>(settings.EmailConfiguration);
        services.AddScoped<MailSender>();
        services.AddControllers();
        services.AddIdentityForWebApi<ApplicationUser, ApplicationRole>(x =>
        {
            x.Password.RequiredLength = 4;
            x.Password.RequireDigit = false;
            x.Password.RequireNonAlphanumeric = false;
            x.Password.RequireUppercase = false;
        }).AddEntityFrameworkStores<SeatingDBContext>();
         
        services.AddAuthentication(defaultScheme:"Bearer")
        .AddIdentityServerAuthentication("Bearer", config =>
        {
            config.Authority = settings.ODICSettings.Authority;
            config.ApiName = settings.ODICSettings.Audience;  
        });
        services.AddAuthorization(options =>
        {
            options.AddPolicy("ApiScope", policy =>
            {
                policy.RequireAuthenticatedUser();
                foreach (string scope in settings.ODICSettings.scope)
                    policy.RequireClaim("scope", scope);
            });
        });
    }

【问题讨论】:

    标签: c# asp.net-core identityserver4 asp.net-core-identity


    【解决方案1】:

    您不能将签名证书用作 HTTPS Web 证书。签名证书仅在 IdentityServer 签署 JWT 令牌时使用。

    您需要从受信任的提供商(如 Lets Encrypt)处获取真正的证书,并将其作为 TLS/HTTPS 证书单独安装。

    签名证书和 TLS/HTTPS 证书是不同的东西,都需要正确配置。

    【讨论】:

    • 我知道我们不能使用 https 证书进行签名......这就是为什么我使用问题中提到的博客文章生成证书并将其上传到 Azure 存储......这两个证书都不同.
    • 您知道您不能使用博文中的技术来生成 HTTPS 证书吗?您是如何获得 HTTPS 证书的?该博客文章仅讨论签名证书。
    • 它托管在 Azure 中,我的基础架构团队在域中提供了该证书。博客文章说“为身份服务器生成签名证书,因此我创建了该证书并将其上传到 Azure 应用服务跨度>
    • 不,它是令牌的签名证书,您可以将其作为 HTTPS 证书上传。它对此无效。签名证书很重要,你可以将它存储在 Azure 中,就像在 Azure Key Vault 中一样,因为你需要它在整个部署过程中都相同。但它仅用于签名密钥。
    • 要清楚,我有一个不同的 https cer (www.domain.com.pfx),我上传了一个证书 rsacert.pfx 作为服务中的私有证书..第一个是用于https,上传的1作为登录证书...
    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 2011-03-28
    • 2016-11-28
    • 2010-10-21
    • 2013-08-08
    • 2012-07-20
    相关资源
    最近更新 更多