Identityserver4 使用不同的服务器验证令牌?
我正在使用IdentityServer4保护一些WebAPI。由于客户设置中的一个怪癖,我们需要支持这样的设置:我们将有多个运行IdentityServer4的应用程序向不同的客户机颁发令牌。然而,这些客户最终会调用一些公共服务 因此,这些公共服务需要能够验证来自IdentityServer4的多个实例的令牌。由于您在启动时向资源服务器注册了IdentityServer实例,我认为只要所有IdentityServer都以相同的方式对令牌进行签名,它就可以工作 我假设将IdentityServer设置为使用共享X509证书将允许在配置为使用不同IdentityServer的资源服务器上验证来自一个IdentityServer的令牌,但情况似乎并非如此。从Server1请求令牌,然后使用Server2将其提交到资源服务器,这是不可行的,即使它们都使用相同的证书Identityserver4 使用不同的服务器验证令牌?,identityserver4,Identityserver4,我正在使用IdentityServer4保护一些WebAPI。由于客户设置中的一个怪癖,我们需要支持这样的设置:我们将有多个运行IdentityServer4的应用程序向不同的客户机颁发令牌。然而,这些客户最终会调用一些公共服务 因此,这些公共服务需要能够验证来自IdentityServer4的多个实例的令牌。由于您在启动时向资源服务器注册了IdentityServer实例,我认为只要所有IdentityServer都以相同的方式对令牌进行签名,它就可以工作 我假设将IdentityServer
有没有办法让这一切顺利进行 您使用的是JWT还是引用令牌 引用令牌由持有身份服务器的机构进行验证。可能很难使用不同的身份服务器并验证由另一个身份服务器提供的令牌,而不是验证它的身份服务器。如果您使用的是JWT,则可以使用发现端点捕获公钥。那么你应该可以用它来验证签名 您可以这样做:
// Define the client to access the IdentityServer Discovery-Endpoint
var discos = new DiscoveryClient(ConfigurationManager.AppSettings["IdentityserverLocation"]);
var disco = await discos.GetAsync();
// get the public key from the discovery-endpoint
var keys = disco.KeySet.Keys;
//Build the authorization request
//param: Disco.AuthorizeEndpoint --> retrieves the authorization url from the identityserver
var request = new AuthorizeRequest(disco.AuthorizeEndpoint);
var url = request.CreateAuthorizeUrl(
clientId: ConfigurationManager.AppSettings["ClientId"],
responseType: "id_token",
scope: "openid profile email",
responseMode: OidcConstants.ResponseModes.FormPost,
redirectUri: ConfigurationManager.AppSettings["RedirectUrl"],
state: CryptoRandom.CreateUniqueId(),
nonce: CryptoRandom.CreateUniqueId());
//Try to initiate validation
try
{
// Check if the token data exists in the request, parse is to a correct token
var idToken = Request.Form.Get("id_token");
JwtSecurityToken j = new JwtSecurityToken(idToken);
var keylist = new List<SecurityKey>();
foreach (var webKey in disco.KeySet.Keys)
{
var exp = Base64Url.Decode(webKey.E);
var mod = Base64Url.Decode(webKey.N);
var key = new RsaSecurityKey(new RSAParameters() { Modulus = mod, Exponent = exp });
keylist.Add(key);
}
//define the parameters for validation of the token
var parameters = new TokenValidationParameters
{
ValidIssuer = disco.Issuer,
ValidAudience = "viper",
IssuerSigningKeys = keylist,
};
var handler = new JwtSecurityTokenHandler();
handler.InboundClaimTypeMap.Clear();
//validate the token using the defined parameters, return the token when validation is succesful
var user = handler.ValidateToken(j.RawData, parameters, out var validatedtoken);
//定义客户端以访问IdentityServer发现端点
var discos=new DiscoveryClient(ConfigurationManager.AppSettings[“IdentityserverLocation]”);
var disco=await discos.GetAsync();
//从发现终结点获取公钥
var keys=disco.KeySet.keys;
//生成授权请求
//param:Disco.AuthorizeEndpoint-->从identityserver检索授权url
var请求=新授权请求(disco.AuthorizeEndpoint);
var url=request.CreateAuthorizeUrl(
clientId:ConfigurationManager.AppSettings[“clientId”],
响应类型:“id_令牌”,
范围:“openid配置文件电子邮件”,
responseMode:OidcConstants.ResponseModes.FormPost,
重定向URI:ConfigurationManager.AppSettings[“重定向URL”],
状态:CryptoRandom.CreateUniqueId(),
nonce:CryptoRandom.CreateUniqueId());
//尝试启动验证
尝试
{
//检查请求中是否存在令牌数据,解析为正确的令牌
var idToken=Request.Form.Get(“id_-token”);
JwtSecurityToken j=新的JwtSecurityToken(idToken);
var keylist=新列表();
foreach(disco.KeySet.Keys中的var webKey)
{
var exp=Base64Url.Decode(webKey.E);
var mod=Base64Url.Decode(webKey.N);
var key=new-RsaSecurityKey(new-rsapameters(){module=mod,index=exp});
keylist.Add(key);
}
//定义令牌验证的参数
var参数=新的令牌验证参数
{
ValidisUser=迪斯科发行人,
ValidAudience=“毒蛇”,
IssuerSigningKeys=密钥列表,
};
var handler=新的JwtSecurityTokenHandler();
handler.InboundClaimTypeMap.Clear();
//使用定义的参数验证令牌,验证成功时返回令牌
var user=handler.ValidateToken(j.RawData,parameters,out var validatedtoken);
在配置资源服务器以使用Identity server进行身份验证时,您需要指定范围和权限等内容…您的所有Identity server是否都具有与URI中相同的“权限”。希望看到此问题的答案…情况类似,但使用指向同一Identity server实例的两个域名ce-这也不起作用,因为您必须将Api资源的“Auhority”设置为单个URI。