【问题标题】:Identity Server 4 and User.Claims.Properties.Count always is 0 on clientIdentity Server 4 和 User.Claims.Properties.Count 在客户端上始终为 0
【发布时间】:2020-09-25 01:04:49
【问题描述】:

我需要在客户的索赔中填写“属性”。 我正在 ProfileService 类中写下关于 IS4 服务器的声明:

public async Task GetProfileDataAsync(ProfileDataRequestContext context)
 {
 // ...
 Claim claim = new Claim("userData", "personalRights");
    string valuePersonalRights = JsonConvert.SerializeObject(userRights);
    claim.Properties.Add(GetKeyValuePair("rights", valuePersonalRights));
    claims.Add(claim);

 context.IssuedClaims.AddRange(claims);
 }

private KeyValuePair<string, string> GetKeyValuePair(string key, string value)
{
   KeyValuePair<string, string> keyValuePair = new KeyValuePair<string, string>(key, value);

   return keyValuePair;
}

在服务器上的此声明中有记录“属性”: https://postgres-russia.ru/wp-content/files/is4_img/on_server.jpg

但是,在客户端,此声明的属性缺失: https://postgres-russia.ru/wp-content/files/is4_img/on_client.jpg

客户端配置:

 services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")
                .AddOpenIdConnect("oidc", options =>
                {
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;
                    options.ClientId = "mvc";
                    options.ClientSecret = "secret";
                    options.ResponseType = "code";
                    options.Scope.Add("openid");
                    options.Scope.Add("profile");
                    options.Scope.Add("email");
                    options.Scope.Add("domainGroups");
                    options.Scope.Add("geolocation");
                    options.Scope.Add("fullname");
                    options.SaveTokens = true;
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = "name"
                    };
                });

如何在客户端获取声明属性?

【问题讨论】:

  • 您的客户端是否需要经过身份验证的用户声明?例如用户名或电子邮件是什么?
  • System.Security.Claims.Claim 具有属性 IDictionary System.Security.Claims.Claim.Properties。我在身份验证服务器上创建了一个声明,并将一些信息放入 System.Security.Claims.Claim.Properties。身份服务器声明中的属性计数器: 1. 当我在客户端获得一组声明时,此声明的属性计数器为 0。如何传输声明的属性?

标签: identityserver4


【解决方案1】:

当您定义您的客户端时,您也可以分配它的声明,这将包含在访问令牌中。

        public static IEnumerable<Client> Clients =>
        new Client[]
        {
            new Client
            {
                ClientId = "spa",
                ClientName = "SPA Client",
                ClientUri = "",
                AllowedGrantTypes = {GrantType.ResourceOwnerPassword,GrantType.ClientCredentials},
                RedirectUris =
                {
                },
                RequireClientSecret = false,
                // secret for authentication
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                PostLogoutRedirectUris = { "" },
                AllowedCorsOrigins = { "","" },
                AllowedScopes = { "openid", "profile","roles", IdentityServerConstants.LocalApi.ScopeName },
                Claims = new Claim[]//look at this property
                {
                    new Claim("prop1","value1")
                }
            }
        };

【讨论】:

  • claims.SelectMany(c=>c.Properties) 客户端没有数据
  • 请分享您的代码。我不能很好地理解你的问题,谢谢。
【解决方案2】:

这是使用 Net Core 时的常见问题。出于某种原因,Firefox 浏览器只是静默,没有显示错误,Chrome 浏览器指向 431 Request Header Fields Too Large。 cookie 中语句的大小超过 4096 字节。通过使用 ITicketStore 并将声明存储为用户会话中的声明来解决。更多详情:stackoverrun.com/ru/q/11186809

更多:IIS Deployed ASP.NET Core application giving intermittent 431 Request headers too long error

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2020-03-22
    • 2019-07-25
    • 2020-01-11
    • 2018-12-04
    • 2019-06-06
    • 2019-02-15
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多