Search code examples
c#asp.nettokenowinowin-middleware

Return custom type with Bearer Tokens using Owin in WebApi2


I have a requirement to implement OWIN authentication in my web api project. I am able to do the same, and i am also able to return multiple Claims. But i want to return UserDetails type values after successfully login where i am struggling.

public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{

}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("UserName", userDetails.UserName));
        identity.AddClaim(new Claim("Iata", userDetails.Iata));
        context.Validated(identity);

        UserDetailsDTO userDetails = UserDetailService.GetUserdetailExternal(context.UserName, context.Password, userDetails.Iata);

       // I want to return userDetails where i need help

}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
    {
        context.AdditionalResponseParameters.Add(property.Key, property.Value);
    }
    return Task.FromResult<object>(null);
}

public class UserDetailsDTO
{
    public string UserName { get; set; }
    public string Iata { get; set; }
    public string LoginId { get; set; }
    public Screen Screens { get; set; }
    public string ErrorText { get; set; }
    public string Roles { get; set; }
    public bool IsADUser { get; set; }
}

public class Screen
{
    public string ScreenName { get; set; }
    public string ScreenUrl { get; set; }

}

JSON output

"access_token": "pG-Ho6S_0-JGdBcUwmtuGBpuoAECjC8T0bs93_FtmOw7dzUezgTRHrbyos67DM8xJmDrjF8vHVBXNlyRUoHxABFA2NOdMvtYPOaoKNMAES1jPN6gWcOQwZN5Tlcyc4qYkXCUGnVuVFncLZIvf0aBIZwUKiJDW4OxCJuh7wRoe49XUCh3hCd8-8R6ZcTy3VKGQlJmLau1pFaHZsqMeGdvbPvxSmixptGp29u76QThnovr8XMmh65f9o1JWDer6CsQSnd2VG1NEkrkhBWur2Jyz6EdEyW-ZbVeEvlw28P_i899RTEjhTBKBlo7m8sLJKViGDEO-uW0r9iBDThp2F-V-0ggp_wfsBHQDbTjqoddSITFAxwe7Vpm1BQAptyF_-Fu2oV-CbzVWucD8dr-PQKBlN8m3tFv4lMO2681X9bApF5yivkDvEMYW5_cNO5hzIOvsQ8hUh7oY5iF-mVZkUgXHmk9pqbAYQ9evTK3MCtDS-XLKqvdkb90BwDFYW6jKg_Sv7IcuYbHv7i5jTwSM8K6MySyjha_Y_4y1VVhx5QSe4afFnH_Z5nNimrGDJgi5mZbRbiKDFeR28qjxXZWmJ8ISyxQRZmT_JVZArGDpu-hVoUcz9CDLvHeCcL2a9uXmLm0",
  "token_type": "bearer",
  "expires_in": 86399,
  ".issued": "Thu, 22 Mar 2018 15:35:56 GMT",
  ".expires": "Fri, 23 Mar 2018 15:35:56 GMT",
  "UserName": "rmishra",
  "Iata": "AU"

I am not able to get userDetails in my JSON object with bearer_token. can anyone help me to return the same?


Solution

  • You need to assign the userdetails json string in authentication property then generate authentication ticket and validate the authenticate ticket.

    UserDetailsDTO userDetails = UserDetailService.GetUserdetailExternal(context.UserName, context.Password, userDetails.Iata);
    
    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
    identity.AddClaim(new Claim("UserName", userDetails.UserName));
    identity.AddClaim(new Claim("Iata", userDetails.Iata));
    IDictionary<string, string> authProp = new Dictionary<string, string>()
    {
        { "UserName", userDetails.UserName },
        { "Iata", userDetails.Iata},
        { "UserDetails", Newtonsoft.Json.JsonConvert.SerializeObject(userDetails)}
    };
    var ticket = new AuthenticationTicket(identity, authProp);
    context.Validated(ticket);