2015-08-23 11 views
9

Ho creato un nuovo progetto Web Api, ha aggiunto Asp.Net Identità e OAuth configurato in questo modo:Come mostrare WebAPI OAuth endpoint gettone in Swagger

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

Questo tutto funziona benissimo, posso chiamare il/Endpoint token e ottenere indietro un token al portatore.

Il problema è che questo non è rilevabile in Swagger. Presumo perché non è su un controller e quindi non ha una documentazione xml generata per esso.

Qualcuno sa di un modo per visualizzare questo endpoint di accesso nei miei documenti Swagger?

Grazie.

Inoltre, avrei dovuto dire che la documentazione di Swagger funziona con tutti i miei controller, è solo che mi manca questo metodo ovvio - come accedere.

+0

Non so se questo [Link] (http://bitoftech.net/2014/08/25/asp-net-web-api -documentation-using-swagger /) aiuterà o meno ma spiegherà anche come creare un XML se non ce n'è uno – KhawajaAtteeq

+0

Grazie, in realtà è uno degli articoli che ho seguito per configurare Swagger. – Russ

+0

Hai menzionato Swashbuckle nei tuoi tag, hai guardato la sezione "Descrivere la sicurezza/Schemi di autorizzazione" di https://github.com/domaindrivendev/Swashbuckle/blob/master/README.md? –

risposta

11

ApiExplorer non genererà automaticamente alcuna informazione per l'endpoint, quindi sarà necessario aggiungere un DocumentFilter personalizzato per descrivere manualmente l'endpoint del token.

C'è un esempio di questo in https://github.com/domaindrivendev/Swashbuckle/issues/332:

class AuthTokenOperation : IDocumentFilter 
{ 
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) 
    { 
     swaggerDoc.paths.Add("/auth/token", new PathItem 
     { 
      post = new Operation 
      { 
       tags = new List<string> { "Auth" }, 
       consumes = new List<string> 
       { 
        "application/x-www-form-urlencoded" 
       }, 
       parameters = new List<Parameter> { 
        new Parameter 
        { 
         type = "string", 
         name = "grant_type", 
         required = true, 
         @in = "formData" 
        }, 
        new Parameter 
        { 
         type = "string", 
         name = "username", 
         required = false, 
         @in = "formData" 
        }, 
        new Parameter 
        { 
         type = "string", 
         name = "password", 
         required = false, 
         @in = "formData" 
        } 
       } 
      } 
     }); 
    } 
} 

httpConfig.EnableSwagger(c => 
{ 
    c.DocumentFilter<AuthTokenOperation>(); 
}); 
+0

esattamente ciò di cui avevo bisogno, grazie – Russ

+1

dove dobbiamo mettere questa classe in ordine per il generatore di HelpPage per creare l'html corretto? – EeKay

+1

@EeKay: molto probabilmente nel tuo SwaggerConfig.cs - è l'ultima parte nell'esempio di codice sopra (httpConfig.EnableSwagger (...)) – Efrain

Problemi correlati