我有ASP.NET网络应用程序,我正在使用IdentityServer3进行用户身份验证。我们的客户使用用户to /密码登录到web应用程序。现在我又有了一个Web,我们的一些客户需要从他们的应用程序中调用这个web。(服务器到服务器通信)。因此,基于这,在身份服务器中,我执行了以下操作
1>创建了一个新的作用域名api
2>为Web创建了一个新客户端,并配置了允许的作用域api
和offline_access
3>集流到ClientCredentials
4>将AccessTokenType设置为Jwt
5> 为每个客户创建了不同的密钥
现在,我们的客户可以在connect/token
端点获取访问令牌,然后使用访问令牌调用API。API用IdentityServer验证令牌,然后返回结果。在这里之前一切都很好。
但是,在API项目中,我还需要标识customer
又名caller
。基于客户,我需要做一些逻辑
public class ResourcesController : ApiController
{
public IHttpActionResult Get()
{
var caller = User as ClaimsPrincipal;
// need to identify caller here
return Json(new
{
message = "OK",
});
}
}
(我可以想到的一个选择是将customer id作为API url的一部分。(类似于http://api.domain.com/v1/customerid/resources
__)
是否需要使用IdentityServer来识别客户?
发布于 2018-12-28 15:23:33
一段时间前我也有类似的需求。对于最简单的解决方案,您应该能够为您为客户创建的每个Identity Server客户端分配一个自定义声明。
AlwaysSendClientClaims = true,
Claims = new List<Claim>()
{
new Claim("CustomerId", "0121021")
}
然后,这些客户端声明将包含在访问令牌中,因此在后端对您可用。
public class ResourcesController : ApiController
{
public IHttpActionResult Get()
{
var caller = User as ClaimsPrincipal;
// need to identify caller here
var customerId = caller?.Claims.Where(p => p.Type.Equals("CustomerId")).First().Value;
// need to identify caller here
return Json(new
{
message = "OK",
});
}
}
https://stackoverflow.com/questions/53938459
复制相似问题