3

I am working on forgot password functionality. I send email with link which contain special token to user to change password. This token is stored in Sitecore.Security.Accounts.User like custom property. Is it possible to get user by custom token field?

2

No, it's not possible to get user by custom token field in Sitecore OOTB.

You have 2 options:

  1. Special token containing encrypted user name. User uses link, user name is decrypted. You get user by username and checks if the whole token is same as what was in the url. Simple and fast.
  2. When user uses url with token, you retrieve ALL the users and finds the one you're interested in by comparing custom property. I don't like it but if you can't change how the token is generated, this can be the only option.
  • 1
    Even though I elaborated on option2 - on a system with lots of users option1 surely seems the faster one ;) – Gatogordo Nov 18 '16 at 10:26
  • Totally agree. I would never go with option 2 if I had a choice. – Marek Musielak Nov 18 '16 at 10:28
  • You may want to consider creating a separate table with the username token mapping. The table will be fast to query and easy to expire. The token should be time-boxed and purged after an elapsed time; trying to query all your users for a custom property may not be too efficient. – Michael West Nov 20 '16 at 3:51
0

You should be able to get it like this:

UserManager.GetUsers().Where(u => u.Profile.GetCustomProperty("--customPropertyName--").Equals("--value--"));

which is actually the 2nd option Marek mentioned. You are getting all users and filtering on that list. Not sure what this would do with a lot of users.. haven't done this myself.

0

Technically it is possible to query on a custom property as the other answers have pointed out. However, I would not loop over all users. Maybe you don't have a lot of users right now, but looping over all users is just not future proof.

An alternative could be to keep some kind of an index in a custom table which has the username and the token linked to each other.

On user:created, user:updated and user:deleted (and their :remote relatives) you update the custom table accordingly. When you need to process the link with the token, you query the custom table for the token and load the Sitecore user by name.

Sitecore.Security.Accounts.User.FromName(username, false);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.