Hi Great Minds,
I am trying to customize the Auth response, which we received using this end point json/reply/authenticate
. Below is the default response mentioned and the desired response.
Default response
{
"userId": "1",
"sessionId": "qYZdSbOEAgb9bBALxOvw",
"userName": "admin@email.com",
"displayName": "Admin User",
"bearerToken": "eyJ0eXA.....",
"refreshToken": "eyJ0eXA....",
"profileUrl": "data:ima.....",
"roles": [
"Admin"
],
"permissions": []
}
Desired response
{
correlationId:123456987,
errorCdoe:0,
"userId": "1",
"sessionId": "qYZdSbOEAgb9bBALxOvw",
"userName": "admin@email.com",
"displayName": "Admin User",
"bearerToken": "eyJ0eXA.....",
"refreshToken": "eyJ0eXA....",
"profileUrl": "data:ima....."
}
To achieve the desired response, I have gone through docs and some stackoverflow QA here.
Now following your suggestion, I have managed to add additional properties in the meta.
this.GlobalResponseFilters.Add((req, res, responseDto) =>
{
if (res.Dto.GetType() == typeof(AuthenticateResponse))
{
CustomUserSession cs = (CustomUserSession)req.GetSession();
Dictionary<string, string> otherData = new Dictionary<string, string>();
otherData.Add("CustomData", cs.CustomData.ToString());
((AuthenticateResponse)res.Dto).Meta = otherData;
}
});
However I am also interested to know that how can I extend the AuthenticateResponse or the AuthService and accommodate my logic directly and use it in my application. Kindly advise.
Below is the ConfigureAuth class just FYR.
public class ConfigureAuth : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services => {
services.AddSingleton<ICacheClient>(new MemoryCacheClient()); //Store User Sessions in Memory Cache (default)
})
.ConfigureAppHost(appHost => {
var appSettings = appHost.AppSettings;
appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new BasicAuthProvider(appSettings),
new JwtAuthProvider(appSettings)
{
AuthKey=AesUtils.CreateKey(),
UseTokenCookie=false,
RequireSecureConnection=false,
ExpireTokensIn=TimeSpan.FromSeconds(60),
ExpireRefreshTokensIn=TimeSpan.FromSeconds(120),
}
}));
appHost.Plugins.Add(new RegistrationFeature()); //Enable /register Service
//override the default registration validation with your own custom implementation
appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>();
});
}