wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

How to check if user exists? #282

Closed sponi78 closed 4 months ago

sponi78 commented 2 years ago

Is there a way to check if a user with a give username and password exists?

navjot50 commented 2 years ago

As far as I know, there is no explicit method that can test both the username and password of a user. But you can simply try to generate a JWT token with your credentials and if it generates a valid token, then a user with the given credentials exists.

You can simply do:

var client = new WordPressClient(ApiCredentials.WordPressUri);
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken(ApiCredentials.Username,ApiCredentials.Password);

// check if authentication has been successful
var isValidToken = await client.IsValidJWToken();

and if isValidToken is true then the user with the credentials exists.

hbcondo commented 4 months ago

I've been using the Get Current User method to accomplish this: https://wp-net.github.io/WordPressPCL/I%20version%202.x/entities/users/

If the submitted username/password does not work, the method will throw an exception. If if does match, then a User object is returned.