RefreshToken undefined after successful Authentication ServiceStack

女生的网名这么多〃 提交于 2019-12-02 06:53:43

问题


  • ServiceStack Version 5.0
  • ServiceStack Typescript Client Version 0.0.40

The following code calls my Auth microservice and successfully authenticates a user and returned there bearer token:

var request = new Authenticate();
request.provider = "credentials";
request.userName = userName;
request.password = password;
request.useTokenCookie = true;

this.client.post(request)
.then(res => {
    if (res.bearerToken != null) {
    console.log('auth success', res);
    console.log('auth refresh token', res.refreshToken);
    resolve(true);
    }
    resolve(false);
}, msg => {
    console.log('log in failed');
    reject(msg);
})

The problem is there is no refresh token return from my Auth microservice:

My configuration for my Auth Microservice:

Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new JwtAuthProvider
        {
            HashAlgorithm = AuthSettings.HashAlgorithm,
            RequireSecureConnection = requireSecureConnection,
            AuthKeyBase64 = AuthSettings.JwtAuthKeyBase64,
            ExpireTokensIn        = TimeSpan.FromHours(_configuration["AuthSettings:ExpireTokensIn"].ToDouble()),
            ExpireRefreshTokensIn = TimeSpan.FromHours(_configuration["AuthSettings:ExpireRefreshTokensIn"].ToDouble()),
            CreatePayloadFilter = (payload,session) => {
                    payload["zipCode"] = ((CustomUserSession)session).ZipCode;
            },
            PopulateSessionFilter = AuthSettings.PopulateSessionFilterImplementation
        },
        new CustomCredentialsAuthProvider((ITsoContext)_serviceProvider.GetService(typeof(ITsoContext))) //HTML Form post of User/Pass
    }));

回答1:


If you're overriding CredentialsAuthProvider with a Custom AuthProvider make sure you're also setting Authentication was performed with:

authService.Request.Items[Keywords.DidAuthenticate] = true;

Support for JWT RefreshTokens also requires a registered AuthRepository.

If you're using a Custom AuthProvider without an IAuthRepository I've just added support for implementing IUserSessionSource as an alternative source to populating Sessions in new JWT Tokens, which you can utilize by registering it in your IOC or having your Custom AuthProvider implement:

public interface IUserSessionSource
{
    IAuthSession GetUserSession(string userAuthId);
}

The implementation should only return a populated IAuthSession if they're allowed to sign-in, i.e. if their account is suspended it should throw an Exception like:

throw HttpError.Forbidden("User is suspended");

Support for IUserSessionSource is available from ServiceStack v5 that's now available on MyGet. Please review v5 changes before upgrading.



来源:https://stackoverflow.com/questions/47403428/refreshtoken-undefined-after-successful-authentication-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!