Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change DateTime format of OWIN .expiration and .issued dates

I started with the "Individual User Accounts" authentication option when creating a .NET Web API starter template (as described here). The token is being correctly generated, however the ".issued" and ".expires" property are in a non-ISO date format. How do I format them with DateTime.UtcNow.ToString("o") so it's ISO 8601 compliant?

{
  "access_token": "xxx",
  "token_type": "bearer",
  "expires_in": 1199,
  "userName": "[email protected]",
  "Id": "55ab2c33-6c44-4181-a24f-2b1ce044d981",
  ".issued": "Thu, 13 Aug 2015 23:08:11 GMT",
  ".expires": "Thu, 13 Aug 2015 23:28:11 GMT"
}

The template uses a custom OAuthAuthorizationServerProvider and provides a hook to add additional properties to the outgoing token (the 'Id' and 'userName' are my props), but I don't see any way to change existing properties.

I did notice that in the override ofTokenEndpoint, I get a OAuthTokenEndpointContext that has a properties dictionary with the .issued and .expired keys. Trying to change these values, however, has no effect.

Thanks much in advance.

like image 530
tobyb Avatar asked Aug 14 '15 16:08

tobyb


1 Answers

AuthenticationProperties class is defined in Microsoft.Owin.Security namespace in Microsoft.Owin.dll.

The setter of IssuedUtc property does the following (for ExpiresUtc is similar):

this._dictionary[".issued"] = value.Value.ToString("r", (IFormatProvider) CultureInfo.InvariantCulture);

As you can see, when setting the IssuedUtc the .issued field of dictionary is set too, and with "r" format.

You can try to do the following in the TokenEndPoint method:

foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
    if (property.Key == ".issued")
    {
        context.AdditionalResponseParameters.Add(property.Key, context.Properties.IssuedUtc.Value.ToString("o", (IFormatProvider) CultureInfo.InvariantCulture));
    }
    else if (property.Key == ".expires")
    {
        context.AdditionalResponseParameters.Add(property.Key, context.Properties.ExpiresUtc.Value.ToString("o", (IFormatProvider) CultureInfo.InvariantCulture));
    }
    else
    {
        context.AdditionalResponseParameters.Add(property.Key, property.Value);
    }
}

I hope it helps.

like image 57
jumuro Avatar answered Nov 14 '22 04:11

jumuro