I’ve downloaded your base MVC app and cannot figure out how to retrieve custom profile attributes for users. I want to manage users and their profiles from within OKTA, but then following login retrieve profile attributes so I can send them on to another application.
I’ve also tried using the OKTA.Core Library and can’t seem to get the profile attributes using it either.
var usersClient = new UsersClient(“xxxxxx”, new Uri(“xxxxxx”));
var user = usersClient.Get("user@email.com");
var custom = user.Profile.GetProperty(“testattr”);
Just curious, which example MVC app did you download?
The base example shows how to get an ID token, but you’re right - you can’t (out of the box) get custom profile attributes. You can do it by adding the Okta .NET SDK. The newer (1.0 prerelease) library makes it much easier to get profile attributes
var client = new OktaClient(new OktaClientConfiguration
{
OrgUrl = "https://dev-<your id>.oktapreview.com",
Token = "<Your API Token>"
});
var someUser = await client.User.GetUserAsync("<someUserId>");
var custom = user.Profile["testattr"];
There is also a user.Profile.GetProperty<T> method you can use to get primitives like strings, ints, and bools from custom profile attributes.
someUser.Profile should contain the whole user profile (including custom attributes) already. Do you need to copy the profile attributes into some other data structure?
Essentialy. I get the following runtime error when trying to use your code and the alpha SDK. I had to make some minor tweaks to correct objects/methods.
var client = new OktaClient(new OktaClientConfiguration
{
OrgUrl = "https://dev-494364-admin.oktapreview.com",
Token = "<my token>"
});
var user = client.Users.GetUserAsync("<emailaddy>").Result;
var custom = user.Profile["foobar"];
There’s a single bad line of code that needs to be removed from the alpha SDK. If you have a Github account, you can comment or subscribe on the issue above and you’ll get notified when it’s fixed.
In the meantime, if you want to use the legacy (0.3x) SDK, this equivalent code should work:
var user = usersClient.Get("user@your_org.com");
var custom = user.Profile.GetProperty("custom_attribute");
This looks very similar to what you had tried originally, so let me know if that doesn’t work for you.
The same PlatformNotSupportedException? I just tried Okta.Core.Client 0.3.3 in an ASP.NET MVC project targeting .NET Framework 4.6.1 and it worked okay. Can you give me more details on the exception you’re seeing? It might be a different bug I need to squash.
Let me know what issues you run into. We’re actively working on improving the aspnet and aspnetcore stuff! Any and all feedback is helpful.
@nate.barbettini - I was trying one of the previous alpha versions, not 0.3.3 of Okta.Core.Client. it’s a bit confusing since https://developer.okta.com/code/dotnet/ links to the new SDK and documentation on Okta.Core.Client is buried a bit.
I was able to use the UsersClient to get a custom property value. Thanks.
@jbd4jc I just noticed another problem - your Org URL is incorrect.
https://dev-494364-admin.oktapreview.com
should be
https://dev-494364.oktapreview.com
The -admin version is the URL of the web developer console, but can’t actually accept API calls.
The PlatformNotSupportedException bug is a separate thing, but since that is now fixed, take a look at the Org URL if you’re still having issues. I should have caught that before, it was staring me in the face!