2016-07-07 77 views
0

我正在使用swashbuckle將swagger添加到我的asp.net mvc web api項目中。我看到了添加OAuth2的選項,但我的網站需要安裝。我如何要求身份驗證以wsfederation查看swagger ui?在SwaggerConfig文件我應該如何將身份驗證添加到swashbuckle?

GlobalConfiguration.Configuration 
    .EnableSwagger(c => 
    { 
     c.SingleApiVersion("v1", "Services"); 
    }) 
    .EnableSwaggerUi(c => { }); 

回答

1

認證是直接關係到記錄您的API,而不是實際執行中,可以這麼說。 所以,如果你使用你的swaggerConfig如下:

c.OAuth2("oauth2") 
       .Description("OAuth2 Implicit Grant") 
       .Flow("implicit") 
       .AuthorizationUrl("http://petstore.swagger.io/oauth/dialog") 
       .Scopes(scopes => 
       { 
        scopes.Add("read:pets", "read your pets"); 
        scopes.Add("write:pets", "modify pets in your account"); 
       }); 

這將產生在招搖JSON文件中的以下securitydefinition

securityDefinitions: 
    petstore_auth: 
    type: oauth2 
    authorizationUrl: 'http://petstore.swagger.io/oauth/dialog' 
    flow: implicit 
    scopes: 
     'write:pets': modify pets in your account 
     'read:pets': read your pets 

要回答

我怎麼能要求身份驗證用 wsfederation查看swagger ui?

只需添加全球認證的WebApiConfig.cs文件,類似於以下(如果您正在使用的MessageHandler或過濾器)

config.Filters.Add(new WSFederationAuthentication()); 

查看招搖文檔直接的WebAPI相關的東西。

雖然您可能會遇到一些問題,因爲Swagger獲得了文檔客戶端。

相關問題