3

In ServiceStack, I am using JwtAuthProvider, where I got Bearer Token and Refresh token so how do I verify the token and authorize the web api service? Code:

var client = new JsvServiceClient(ListeningOn) { UserName = "tuser", Password = "password" };
            client.Send<AssignRolesResponse>(new AssignRoles
            {
                UserName = "tuser",
                Roles = new ArrayOfString("TestRole"),
                Permissions = new ArrayOfString("GetStatus")
            });

            var jwtToken = client.Send(new Authenticate()).BearerToken;

Here, What is the use of 'jwtToken' value? user is already authorized and authenticated so I don't get the idea why the token needed here?

Could anyone suggest me how do I take advantage of that token? JWT Config:

this.Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                                new IAuthProvider[]
                                    {
                                        new JwtAuthProvider(AppSettings) {
                                        RequireSecureConnection = false,
                                        AuthKey = AesUtils.CreateKey(),
                                        //CreatePayloadFilter = (payload,session) =>
                                       // payload["CreatedAt"] = session.CreatedAt.ToUnixTime().ToString(),
                                        CreatePayloadFilter = (jwtPayload, session) =>
                jwtPayload["exp"] = DateTime.UtcNow.AddSeconds(-1).ToUnixTime().ToString()
        },
                                new CredentialsAuthProvider(AppSettings),
                                new BasicAuthProvider()
                    }));

1 Answer 1

2

Please see this article on the purpose of JWT Refresh Tokens.

In summary, the BearerToken the actual JWT and what used to authenticate via JWT. It's contains a stateless snapshot of the Users Session and is typically a short-lived token, which after it expires needs to contact the Auth Server to fetch a new JWT Token.

The RefreshToken is a longer-lived token which can be used to request new JWT Tokens transparently (i.e without forcing the User to manually re-authenticate) and will be populated with the latest snapshot of the Users Session.

The shorter the lifetime of JWT BearerTokens the less stale the stateless Session information that's stored in the JWT but the more often the RefreshToken will need to be used to fetch an updated BearerToken. Only after a new BearerToken is requested will session information like the Roles and Permissions has or whether they're locked out.

The lifetime of each Token is configurable with the ExpireTokensIn and ExpireRefreshTokensIn JwtAuthProvider properties.

ServiceStack Clients built-in support for JWT and Refresh Tokens

ServiceStack's Service Clients automatically take care of transparently fetching new JWT Tokens using RefreshTokens. You would typically populate both the BearerToken and RefreshToken when initializing your Service Client, e.g:

var authResponse = authClient.Send(new Authenticate());

var client = new JsonServiceClient(baseUrl) {
    BearerToken = authResponse.BearerToken,
    RefreshToken = authResponse.RefreshToken,
};

The BearerToken is needed to make the request although since the Service Client automatically fetches new JWT Tokens with the configured RefreshToken you only need to populate the RefreshToken:

var client = new JsonServiceClient(baseUrl) {
    RefreshToken = authResponse.RefreshToken,
};

As ServiceStack will automatically fetch a new JWT Token on first use, but you can save a round-trip by populating both.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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