OWIN de Sécurité - Comment mettre en place OAuth2 Actualisation des Jetons

Je suis à l'aide de l'Api du Web 2 modèle est livré avec Visual Studio 2013 a quelques OWIN middleware pour faire de l'Authentification de l'Utilisateur et les goûts de.

Dans le OAuthAuthorizationServerOptions j'ai remarqué que le OAuth2 Serveur est configuré pour distribuer les jetons qui expire dans 14 jours

 OAuthOptions = new OAuthAuthorizationServerOptions
 {
      TokenEndpointPath = new PathString("/api/token"),
      Provider = new ApplicationOAuthProvider(PublicClientId,UserManagerFactory) ,
      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
      AllowInsecureHttp = true
 };

Ce n'est pas adapté pour mon dernier projet. Je tiens à main de courte durée bearer_tokens qui peuvent être actualisées à l'aide d'un refresh_token

J'ai fait beaucoup de recherches sur google et ne peut pas trouver quelque chose d'utile.

Donc, c'est comment à présent, j'ai réussi à obtenir. J'ai maintenant atteint le point de "WTF dois-je maintenant".

J'ai écrit un RefreshTokenProvider qui implémente IAuthenticationTokenProvider que par la RefreshTokenProvider bien sur OAuthAuthorizationServerOptions classe:

    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
    {
       private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens = new ConcurrentDictionary<string, AuthenticationTicket>();

        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var guid = Guid.NewGuid().ToString();


            _refreshTokens.TryAdd(guid, context.Ticket);

            //hash??
            context.SetToken(guid);
        }

        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            AuthenticationTicket ticket;

            if (_refreshTokens.TryRemove(context.Token, out ticket))
            {
                context.SetTicket(ticket);
            }
        }

        public void Create(AuthenticationTokenCreateContext context)
        {
            throw new NotImplementedException();
        }

        public void Receive(AuthenticationTokenReceiveContext context)
        {
            throw new NotImplementedException();
        }
    }

    //Now in my Startup.Auth.cs
    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/api/token"),
        Provider = new ApplicationOAuthProvider(PublicClientId,UserManagerFactory) ,
        AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(2),
        AllowInsecureHttp = true,
        RefreshTokenProvider = new RefreshTokenProvider() //This is my test
    };

Alors maintenant, quand quelqu'un demande une bearer_token je suis maintenant en envoyant un refresh_token, ce qui est excellent.

Alors maintenant, comment puis-je utilise cette refresh_token pour obtenir une nouvelle bearer_token, sans doute j'ai besoin d'envoyer une demande à mon jeton de point de terminaison de certains en-Têtes HTTP ensemble?

Il suffit de penser à haute voix que je tape... dois-je les traiter refresh_token expiration dans mon SimpleRefreshTokenProvider? Comment un client d'obtenir un nouveau refresh_token?

Que je pouvais vraiment faire avec certains matériels de lecture et de documentation, parce que je ne veux pas de se tromper et souhaitez suivre une sorte de standard.

InformationsquelleAutor SimonGates | 2013-12-17