我们正在 Kubernetes 上开发微服务应用程序。微服务之一是 IdentityServer 实例。最初,我想在 Docker 上本地测试该解决方案以确保其有效。为此,我想将证书复制到 appsettings.json。最终,该值将被 Kubernetes 密钥替换。在我的启动课程中,这就是我尝试加载证书的方式:
services.AddIdentityServer()
.AddSigningCredential(GetIdentityServerCertificate())
.AddConfigurationStore(...
private X509Certificate2 GetIdentityServerCertificate()
{
var clientSecret = Configuration["Certificate"];
var pfxBytes = Convert.FromBase64String(clientSecret);
var certificate = new X509Certificate2(pfxBytes, "PasswordHere");
return certificate;
}
Run Code Online (Sandbox Code Playgroud)
该证书是我使用 openssl 生成的:
openssl req –newkey rsa:2048 –nodes –keyout XXXXX.key –x509 –days 365 –out XXXXX.cer
openssl pkcs12 –export –in XXXX.cer –inkey XXXX.key –out XXXX.pfx
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法获得证书:
$pfxFilePath = 'C:\XXXX.pfx'
$pwd = 'PasswordHere'
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$collection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$collection.Import($pfxFilePath, $pwd, $flag)
$pkcs12ContentType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
$clearBytes = …Run Code Online (Sandbox Code Playgroud) 出于未知的原因,访问令牌中不存在“aud”声明(尽管它存在于 id 令牌中)。
将访问令牌发送到 API 后,我收到以下错误:
Bearer 未经过身份验证。失败消息:IDX10214:受众验证失败。观众:“空”。不匹配:validationParameters.ValidAudience:'productconfigurationapi'或validationParameters.ValidAudiences:'null'。
我知道我可以关闭受众验证,然后一切正常,但我不明白为什么“aud”不是访问令牌的一部分。
这是我的 IS4 配置:
客户端:
new Client
{
ClientId = "Spa",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
AlwaysSendClientClaims = true,
AlwaysIncludeUserClaimsInIdToken = true,
AccessTokenType = AccessTokenType.Jwt,
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"productconfigurationapi"
},
RequireConsent = false
}
Run Code Online (Sandbox Code Playgroud)
api资源:
new ApiResource("productconfigurationapi")
{
UserClaims =
{
JwtClaimTypes.Audience
}
}
Run Code Online (Sandbox Code Playgroud)
API 范围:
return new List<ApiScope>
{
new ApiScope("productconfigurationapi")
};
Run Code Online (Sandbox Code Playgroud)
以下是 IS4 在其主机应用程序中的配置方式:
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddConfigurationStore(options =>
{
})
.AddOperationalStore(options =>
{
})
.AddAspNetIdentity<IdentityUser>()
.AddJwtBearerClientAuthentication();
Run Code Online (Sandbox Code Playgroud) 我正在尝试将服务部署到 AKS 上的特定 url 地址。以下 yaml 让我在所需地址访问服务,例如 xxxx.europe.cloudapp.azure.com/service-a。这很好用,我设法将整个服务隐藏在所需的 url 下:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istio-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: istio
spec:
hosts:
- "*"
gateways:
- istio-gateway
http:
- match:
- uri:
prefix: /service-a
rewrite:
uri: /
route:
- destination:
host: service-a.default.svc.cluster.local
Run Code Online (Sandbox Code Playgroud)
但是,当显示欢迎页面时,我只能看到文字。没有加载 css/javascript/image 文件。此页面尝试加载的所有内容仍然具有原始 url 地址,而我的网关配置没有进行任何重写。所以主页要求:
http://xxxxx.europe.cloudapp.azure.com/icon.jpg
Run Code Online (Sandbox Code Playgroud)
而不是这个:
http://xxxxx.europe.cloudapp.azure.com/service-a/icon.jpg
Run Code Online (Sandbox Code Playgroud)
处理页面上资源和链接的重写 url 的最佳方法是什么?我必须手动更改主页上的网址吗?
编辑:
要更清楚。
我遇到的问题反映在以下示例中。我在 BookController 中创建了一个 POST 方法来创建一个 Book 实体:
这是我的代码:
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public virtual ICollection<BookCategory> BookCategories { get; set; }
}
public class BookCategory
{
public int BookId { get; set; }
public virtual Book Book { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的 DTO:
public class BookDto
{
public int BookId { …Run Code Online (Sandbox Code Playgroud)