1

I'm having an issue returning a custom AutenticateResponse in the new version of ServiceStack. This code worked in the previous version of ServiceStack, but after the upgrade it is no longer functioning as expected.

The AuthenticateResponse

public class CustomAuthResponse : AuthenticateResponse
{
    public List<CustomCompanyDTO> Companies { get; set; }
    public List<string> Roles { get; set; }
    public List<string> Permissions { get; set; }
    public string DisplayName { get; set; }
    public string Email { get; set; }
}

The Service

public class CurrentUserService : AppServiceBase
{
    public object Any(CurrentUser cu)
    {
        CustomAuthResponse response = new CustomAuthResponse();

        response.DisplayName = UserSession.DisplayName;
        response.Email = UserSession.Email;
        response.Companies = UserSession.Companies;
        response.UserName = UserSession.UserName;
        response.Roles = UserSession.Roles;
        response.Permissions = UserSession.Permissions;
        return response;
    }
}

In v3 I can call the CurrentUserService and it returns all the data as expected. In v4 when I call CurrentUserService none of the custom fields are included in the response.

I can work around this particular call by changing the code as follows:

public class CurrentUserService : AppServiceBase
{
    public object Any(CurrentUser cu)
    {
        CustomAuthResponse response = new CustomAuthResponse();
        var x = new
        {
            DisplayName = UserSession.DisplayName,
            Email = UserSession.Email,
            Companies = UserSession.Companies,
            UserName = UserSession.UserName,
            Roles = UserSession.Roles,
            Permissions = UserSession.Permissions,
        };
        return x;
    }
}

The above code works as expected. I can certainly change my code to work this way, I'm mostly wondering what has changed as I'm curious if it will impact my code in other places. I'm seeing the same issue when trying to return ny CustomAuthResponse from the Authenticate call my custom CredentialsAuthProvider.

Estyn
  • 695
  • 6
  • 9

1 Answers1

1

The issue is likely that DataContract attributes are now inherited and if a DTO is marked as a [DataContract] it's opt-in and only the properties marked with DataMember are serialized.

As AuthenticateResponse is a DataContract, if you want to re-use the DTO you should mark the properties you want serialized with a [DataMember] attribute, e.g:

[DataContract]
public class CustomAuthResponse : AuthenticateResponse
{
    [DataMember]
    public List<CustomCompanyDTO> Companies { get; set; }
    [DataMember]
    public List<string> Roles { get; set; }
    [DataMember]
    public List<string> Permissions { get; set; }
    [DataMember]
    public string DisplayName { get; set; }
    [DataMember]
    public string Email { get; set; }
}
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390