ASP.NET MVC App - how to get custom profile attributes

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 :slight_smile:

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.

Let me know if this gets you working again!

Thanks Nate, I’ll try that.

Nate, this is the OKTA MVC app I am using as a quick start.

1 Like

Is there a way to get the complete profile - all profile attributes for a user so I don’t have to make multiple calls?

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"];

And I added the .Result just to try to get it working in the controller as is.

Here is the complete controller:

[Authorize]
    public async Task<ActionResult> Claims()
    {
        Debug.WriteLine(HttpContext.User.Identity);

        var client = new OktaClient(new OktaClientConfiguration
        {
            OrgUrl = "https://dev-494364-admin.oktapreview.com",
            Token = "<my token>"
        });

        var user = await client.Users.GetUserAsync("<email addy>");
        var custom = user.Profile["foobar"];


        ViewBag.custom = custom;

        return View(HttpContext.GetOwinContext().Authentication.User.Claims);
    }

Thanks! The detail you posted was helpful in tracking down the issue. Turns out it was already reported on Github too: System.PlatformNotSupportedException: ‘Operation is not supported on this platform.’

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.

Nate - I get the same error in 0.3x.

I’ll see if I can get the asp.net core quickstart running. I experienced issues with it the first time around.

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.

1 Like

Glad you got it working! Sorry about the confusing state of the docs. I’m working on getting it all cleaned up right now.

Don’t hesitate to post again if you run into more issues!

Cheers,
Nate

@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! :slight_smile: