一、該鏈接連接到api中基本的swagge功能:http://www.cnblogs.com/hhhh2010/p/5234016.htmljavascript
2.在swagger中使用驗證(這裏使用密碼驗證模式)http://www.cnblogs.com/WJ--NET/p/7195124.html <---搭建本身的web api驗證服務器html
3.在項目中會自動生成SwaggerConfig和Startup文檔,若是是有驗證服務器的話,swagger的配置就不須要在swaggerconfi中配置了,直接在Startup中配置swagger;java
using System; using System.IO; using System.Reflection; using System.Web.Http; using Microsoft.Owin; using Microsoft.Owin.Security.OAuth; using Owin; using Swashbuckle.Application; [assembly: OwinStartup(typeof(LogicServer.ResourceServer.Startup))] namespace LogicServer.ResourceServer { /// <summary> /// The assebmly should use owin middleware and start at running the Startup method. /// </summary> public class Startup { /// <summary> /// Configuration /// </summary> /// <param name="app">app</param> public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); config.EnableSwagger("docs/{apiVersion}/swagger", c => { c.SingleApiVersion("v1", "昊天企業端接口"); var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; var fileName = Assembly .GetExecutingAssembly() .GetName() .Name + ".XML"; var commentsFile = Path.Combine(baseDirectory, "bin", fileName); c.IncludeXmlComments(commentsFile); }).EnableSwaggerUi(c => { var thisAssembly = typeof(SwaggerConfig).Assembly; c.InjectJavaScript(thisAssembly, "LogicServer.bearerAuth.BearerAuth.js"); //注入js,在js中寫入根據用戶名和密碼獲取token而後添加到接口的Http header中 c.DisableValidator(); }); ConfigureOAuth(app); // set api authentication schema. UnityConfig.RegisterComponents(config); // Use unity as ioc container. Global dependency resolver. WebApiConfig.Register(config); // Setup web api route policy. // SwaggerConfig.Register(); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // Use Cors in message handling. app.UseWebApi(config); } private void ConfigureOAuth(IAppBuilder app) { // Token Consumption app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions { }); } } }
$(function () { var basicAuthUI = '<div class ="input"> 用戶名:<input placeholder ="username" id ="input_username" onchange="addAuthorization();" name ="username" type ="text" size ="15"> </ div>' + '<div class ="input"> 密碼:<input placeholder ="password" id ="input_password" onchange="addAuthorization();" name ="password" type ="password" size ="20"> </ div>'; $('#input_apiKey').hide(); $('#api_selector').html(basicAuthUI); }); function addAuthorization() { var username = document.getElementById("input_username").value; var password = document.getElementById("input_password").value; var data = "grant_type=password&username=" + username + "&password=" + password; $.ajax({ url: "http://168.33.162.189:8889/token", type: "post", contenttype: 'x-www-form-urlencoded', data:data, success: function (response) { var bearerToken = 'Bearer ' + response.access_token; console.log(bearerToken); swaggerUi.api.clientAuthorizations.add('key', new SwaggerClient.ApiKeyAuthorization('Authorization', bearerToken, 'header')); } }); }
參考文檔連接:http://www.jianshu.com/p/3329b4126886web