如何为混合流客户端填充自定义标识资源声明?

时间:2016-12-12 10:59:35

标签: asp.net-mvc claims-based-identity openid-connect identityserver4

我有一个带有自定义标识资源的IdentityServer4服务器:

new IdentityResource
{
    Name = "custom.profile",
    UserClaims = new[] { "location" }
}

此资源由连接OIDC混合流和无同意页的ASP.NET MVC客户端的用户使用:

new Client
                {
                    ClientId = "client.mvc",
                    ClientName = "MVC Core Client",
                    AllowedGrantTypes = GrantTypes.Hybrid,
                    ...
                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "custom.profile"
                    },

登录在<idsrv>/Login/DoLogin

中手动完成
public async Task<IActionResult> DoLogin(LoginInputModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Username == "test.user" && model.Password == "password")
                {
                    Guid userId = new Guid("8da49efb-a1aa-4253-bb7f-56cc6c532b78");

                    await HttpContext.Authentication.SignInAsync(userId.ToString(), model.Username);

                    if (_interaction.IsValidReturnUrl(model.ReturnUrl))
                    {
                        return Redirect(model.ReturnUrl);
                    }

                    return Redirect("~/");
                }

                ModelState.AddModelError("", "Invalid username or password.");
            }

我的问题是,我如何/在哪里填充用户“location”范围的“custom.profile”值?

1 个答案:

答案 0 :(得分:1)

您基本上需要在GetProfileDataAsync服务上实施IProfileService方法来添加一些声明。

所以在启动时请确保你有类似的东西:

identityServerBuilder.Services.AddScoped<IProfileService, ProfileService>();

然后在你的ProfileService课程中有类似的内容:

public Task GetProfileDataAsync(ProfileDataRequestContext context) {
    context.IssuedClaims.Add(new Claim()); //Your claims(s)
}