LoginAsync #
Authenticates a user.
If this action is successful, all further requests emitted by this SDK instance will be in the name of the authenticated user, until either the authenticated token expires, the logout action is called, or the authentication token property is manually unset.
Arguments #
public async Task<JObject> LoginAsync(
string strategy,
JObject credentials,
TimeSpan? expiresIn = null);
Argument | Type | Description |
---|---|---|
strategy | string | Strategy to use |
credentials | JObject | JObject representing the credentials |
expiresIn | TimeSpan? | Token duration |
strategy #
The name of the authentication strategy used to log the user in.
Depending on the chosen authentication strategy
, additional credential arguments may be required. The API request example on this page provides the necessary arguments for the local
authentication plugin.
Check the appropriate authentication plugin documentation to get the list of additional arguments to provide.
expiresIn #
The default value for the expiresIn
option is defined at server level, in Kuzzle's configuration file.
Return #
Returns a JObject with the following properties:
Property | Type | Description |
---|---|---|
_id | string | User's kuid |
jwt | string | Encrypted authentication token, that must then be sent in the requests headers or in the query |
expiresAt | Int64 | Token expiration date, in Epoch-millis (UTC) |
ttl | Int64 | Token time to live, in milliseconds |
Once auth:login
has been called, the returned authentication token is stored by the SDK and used for all the subsequent API call, ensuring they are properly authenticated.
Exceptions #
Throws a KuzzleException
if there is an error. See how to handle error.
Usage #
try {
JObject response = await kuzzle.Auth.LoginAsync(
"local",
JObject.Parse("{username: 'foo', password: 'bar'}"));
Console.WriteLine(response.ToString(Formatting.None));
/*
{
"_id": "foo",
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfa...",
"expiresAt": 1564568979921,
"ttl": 3600000
}
*/
Console.WriteLine("Successfuly logged in");
} catch (KuzzleException e) {
Console.WriteLine(e);
}