Sea*_*ean 5 azure azure-active-directory identityserver4
我已从 github 中获取此示例以尝试使用 IdentityServer4 和 Azure AD 进行身份验证。
虽然我让它工作并返回一个令牌,但我希望从 Azure AD 收到的声明似乎不包含在通过 IdentityServer 发布的令牌中。
这可能是故意的,我误解了这个流程,但我希望用户通过 Azure AD 分配的角色(加上租户 ID 和 Azure 令牌中的其他有用“位”)能够包含在发给客户的代币中。
有人能帮我解释一下吗?我可以在此处粘贴代码,但指向 github 代码的链接与我使用的几乎相同。
我试图做同样的事情,并最终通过查看 IS4 文档、Github 和 StackOverflow 设法将部分拼凑在一起。
您需要配置IProfileService
( Docs )以便告诉 IdentityServer4 您希望将用户身份的哪些附加声明(在您的情况下从 Azure AD 获得)传递回客户端。
一个示例可能如下所示:
public class CustomProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
// Get the 'upn' claim out from the identity token returned from Azure AD.
var upnClaim = context.Subject.FindFirst(c => c.Type == ClaimTypes.Upn);
// Get 'firstname' and 'givenname' claims from the identity token returned from Azure AD.
var givenNameClaim = context.Subject.FindFirst(c => c.Type == ClaimTypes.GivenName);
var surNameClaim = context.Subject.FindFirst(c => c.Type == ClaimTypes.Surname);
// Add the retrieved claims into the token sent from IdentityServer to the client.
context.IssuedClaims.Add(upnClaim);
context.IssuedClaims.Add(givenNameClaim);
context.IssuedClaims.Add(surNameClaim);
}
public Task IsActiveAsync(IsActiveContext context)
{
context.IsActive = true;
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要在以下位置注册此服务Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
// Register the new profile service.
.AddProfileService<CustomProfileService>();
}
Run Code Online (Sandbox Code Playgroud)
最后,在您的内部AccountController.cs
(在 IdentityServer4 项目中 - 我假设您已经有了这个,如果没有,请参见此处的入门设置),您需要将以下内容添加到ExternalLoginCallback()
:
[HttpGet]
public async Task<IActionResult> ExternalLoginCallback()
{
//...
// this allows us to collect any additonal claims or properties
// for the specific protocols used and store them in the local auth cookie.
// this is typically used to store data needed for signout from those protocols.
var additionalLocalClaims = new List<Claim>();
// ADD THIS LINE TO TELL IS4 TO ADD IN THE CLAIMS FROM AZURE AD OR ANOTHER EXTERNAL IDP.
additionalLocalClaims.AddRange(claims);
//...
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
归档时间: |
|
查看次数: |
2293 次 |
最近记录: |