Accessing custom user information during sign-up/sign-in

Christian Sinschek asked on February 2, 2018 10:55

I am creating an MVC application, and I would like to extend user authentication and sign in with additional data to identify subgroups of my users and to redirect a user based on his or her group. I also want to be able to revoke front-end access to a group as a whole.

I have tried to model this with an additional data token (a string). I am now considering how to access this data. My preferred solution would have been to use a custom field as offered by the membership module and to access this field in the MVC application, but it seems that I cannot directly do that without custom SQL (or EF) code, as the Kentico.Membership.User type encapsulates the regular login information without letting me access other data. Is there a way to get to the other user data?

I am also considering to use roles instead to subdivide user groups, but is there a way to select the target URL after login for a user based on his role, again without separately accessing the role information with custom database queries?

Correct Answer

Peter Mogilnitski answered on February 2, 2018 15:34

Something like this

// Gets the user
UserInfo MyUser = UserInfoProvider.GetUserInfo("NewUser");
if (MyUser != null)
    var CustomField = MyUser.GetValue("MyCustomField");

//or update

if (MyUser != null)
    // Updates the user's properties
    // Saves the changes

Check the links in my first answer. It will get you started.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Peter Mogilnitski answered on February 2, 2018 15:11 (last edited on February 2, 2018 15:29)

If you go to modules\Membership\classes you have 2 classes there where you can add your additional data:

You can add your custom field to any of these tables (Don't do it in SQL - do it via Kenitco admin interface). Check out api examples or just take a look at CMS Membership Namespace

P.S. You might as well take a look at membership management and role management but I am guessing you want to do something more custom.

0 votesVote for this answer Mark as a Correct answer

Christian Sinschek answered on February 2, 2018 15:24

Thanks, but that is what I tried: I added a custom field, but my issue is the following. How do I access the custom field from my MVC site? The team would like to avoid using EF or plain SQLConnections for an otherwise simple site, so is there a way to retrieve the additional data without it?

I have looked at membership management, but our solution only requires a simple notion of (disjunct) account types for customers, not composite roles, which is what I understand memberships to be.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.