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?
No, it's not possible to get user by custom token field in Sitecore OOTB.
You have 2 options:
- 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.
- 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.
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.
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.
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.