lvermeulen / Keycloak.Net

C# client for Keycloak 6.x
MIT License
206 stars 121 forks source link

Add support for user attributes #1

Closed Schirmi136 closed 5 years ago

Schirmi136 commented 5 years ago

Hi, I'd like to access and update custom user attributes. Can you extend the user model with public Dictionary<string,string> Attributes { get; set; } ?

lvermeulen commented 5 years ago

Can you give some more info? In which call would you like to access/update custom user attributes?

Schirmi136 commented 5 years ago

Sure: I'm using CreateUserAsync, UpdateUserAsync and GetUsersAsync to access and modify users in Keycloak. Writing attributes is already possible when creating a subclass like

public class ExtendedUser : User
{
    public Dictionary<string, string> Attributes { get; set; }
}

and use an instance of ExtendedUser as parameter for CreateUserAsync or UpdateUserAsync. GetUsersAsync of course only returns a User without attributes for User does not have the 'Attributes' property. It might already be sufficient to add this property to the User class to obtain attributes via GetUsersAsync as well.

lvermeulen commented 5 years ago

Release 1.0.3

timiil commented 5 years ago

hey, bros, the Dictonary<string, string> Attributes is not perfect, when the user had locale attribute,

curl -X GET -H "Authorization: Bearer $token"  http://xxx.com/auth/admin/realms/by/users
[{"id":"8da5b81f-26a5-4de2-8448-c57dc2cffd36","createdTimestamp":1569856345641,"username":"admin","enabled":true,"totp":false,"emailVerified":false,"attributes":{"locale":["zh-CN"]},"disableableCredentialTypes":["password"],"requiredActions":[],"notBefore":0,"access":{"manageGroupMembership":false,"view":true,"mapRoles":false,"impersonate":false,"manage":false}},{"id":"b34ba3f2-9f7a-49e2-8ee0-9a5e0fb4352f","createdTimestamp":1570203179678,"username":"normal","enabled":true,"totp":false,"emailVerified":false,"attributes":{"avator":["11111111111111111"],"picture":["2222222222222222222"]},"disableableCredentialTypes":["password"],"requiredActions":[],"notBefore":0,"access":{"manageGroupMembership":false,"view":true,"mapRoles":false,"impersonate":false,"manage":false}}]

we can see the 'locale' is come as an string[] array.... the 1.0.3 release will failed, the NewstonSoft.Json will not deserailize this guy. so, i had change the source code User.cs :

...

[JsonProperty("attributes")]
 public Dictionary<string, object> Attributes { get; set; }

that will make pass the failed. should i make a PR ? or will you please Mr Ivereulen just change the line, thanks you very much :)

lvermeulen commented 5 years ago

I'll change it and push an update.