In order to share tokens you need to use an ISecureDataFormat<AuthenticationTicket>
that both applications understand.
This can be easily achieved with the default TicketDataFormat
and an implementation of an IDataProtector
that will be able to Protect/Unprotect identically on both sites.
OWIN(Katana) uses a DpapiDataProtector
tied to the CurrentUser and the 'host.AppName' app property which makes it unique unless you set both sites to have the same name and the same user, which is not ideal.
But implementing your own protector is easy enough. Here is an implementation that will protect the data with a LocalMachine scope.
using System.Security.Cryptography;
using Microsoft.Owin.Security.DataProtection;
namespace MyApp.Owin
{
public class LocalMachineDpapiDataProtector : IDataProtector
{
private readonly DpapiDataProtector protector;
internal LocalMachineDpapiDataProtector(string appName, string[] purposes)
{
protector = new DpapiDataProtector(appName, "Microsoft.Owin.Security.DataProtection.IDataProtector", purposes)
{
Scope = DataProtectionScope.LocalMachine
};
}
public byte[] Protect(byte[] userData)
{
return protector.Protect(userData);
}
public byte[] Unprotect(byte[] protectedData)
{
return protector.Unprotect(protectedData);
}
}
}
And here is how to setup both your Authentication server and middleware.
Auth server:
var ticketDataFormat =
new TicketDataFormat(
new LocalMachineDpapiDataProtector(
"MyApp",
new string[] { "OAuth" }));
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
/*... your auth server settings*/
AccessTokenFormat = ticketDataFormat
});
Auth middleware:
//note that we use a data format that is setup in the same manner
var ticketDataFormat =
new TicketDataFormat(
new LocalMachineDpapiDataProtector(
"MyApp",
new string[] { "OAuth" }));
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
/*... your auth middleware settings*/
AccessTokenFormat = ticketDataFormat
});
Note that by using this LocalMachineDpapiDataProtector
you need to deploy both site on the same machine. If not you would need to use an IDataProtector
with a different protection strategy. See this implementation of AesDataProtector
.