1. 程式人生 > >asp.net webapi+swagger+OAuth2.0

asp.net webapi+swagger+OAuth2.0

== status direct lap .get path all innertext stat

文檔繼續完善整理中。。。。。。

技術分享圖片
c.DocumentFilter<SwaggerDocTag>();

 /// <summary>
    /// Swagger註釋幫助類
    /// </summary>
    public class SwaggerDocTag : IDocumentFilter
    {
        /// <summary>
        /// 添加附加註釋
        /// </summary>
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            
//swaggerDoc.tags = new List<Tag> //{ // //添加對應的控制器描述 這個是我好不容易在issues裏面翻到的 // new Tag { name = "Home", description = "後臺" }, // new Tag { name = "Client", description = "客戶端" }, // new Tag { name = "System", description = "系統" }
//}; swaggerDoc.tags = GetControllerDesc(); } /// <summary> /// 從xml註釋中讀取控制器註釋 /// </summary> /// <returns></returns> private List<Tag> GetControllerDesc() { List<Tag> tagList = new List<Tag>();
var xmlpath = string.Format(string.Format("{0}/bin/Eternal.WebAPI.xml", System.AppDomain.CurrentDomain.BaseDirectory)); if (!File.Exists(xmlpath))//檢查xml註釋文件是否存在 return tagList; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlpath); string memberName = string.Empty;//xml三級節點的name屬性值 string controllerName = string.Empty;//控制器完整名稱 string key = string.Empty;//控制器去Controller名稱 string value = string.Empty;//控制器註釋 foreach (XmlNode node in xmlDoc.SelectNodes("//member"))//循環三級節點member { memberName = node.Attributes["name"].Value; if (memberName.StartsWith("T:"))//T:開頭的代表類 { string[] arrPath = memberName.Split(.); controllerName = arrPath[arrPath.Length - 1]; if (controllerName.EndsWith("Controller"))//Controller結尾的代表控制器 { XmlNode summaryNode = node.SelectSingleNode("summary");//註釋節點 key = controllerName.Remove(controllerName.Length - "Controller".Length, "Controller".Length); if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !tagList.Contains(new Tag { name = key })) { value = summaryNode.InnerText.Trim(); tagList.Add(new Tag { name = key, description = value }); } } } } return tagList; }
swagger顯示控制器註釋

  /**
    * Get Code
    */
    AuthHttpClient.prototype.getCode = function (url, client_id, callback, errorCallback, statusCodeCallback) {
        //&scope=admin
        $.ajax(url + ‘authorize?response_type=code&scope=xxx&client_id=‘ + client_id + "&redirect_uri=" + window.location.href, {
            type: ‘POST‘,
            async:false,
            success: function (data, textStatus, jqXHR) {
                if (callback !== null) {
                    callback(data);
                    sessionStorage.setItem("registered_redirect_uri", location.href);
                    location.href = data;
                    return false;
                }
            },
            error: function (xhr, ajaxOptions, thrown) {
                if (errorCallback !== null) {
                    errorCallback(xhr, ajaxOptions, thrown);
                }
            },
            statusCode: statusCodeCallback
        });
    };
    /**
    * Get Implicit
    */
    AuthHttpClient.prototype.getImplicitToken = function (url, client_id, callback, errorCallback, statusCodeCallback) {
        if (location.hash.indexOf(‘access_token‘) !== -1) {
            var urlpar = location.hash.substr(1).split("&");
            for (var key in urlpar) {
                var parm = urlpar[key].split("=");
                sessionStorage.setItem(parm[0], parm[1]);
            }
        }
        else {
            location.href = url + ‘authorize?response_type=token&client_id=‘ + client_id + "&redirect_uri=" + window.location.href;
        }
    };
    /**
    * Get 密碼
    */
    AuthHttpClient.prototype.getPassWordToken = function (url, username, password, client_id, client_secret) {
        $.ajax(url + ‘token‘, {
            data: {
                ‘grant_type‘: ‘password‘,
                ‘username‘: username,
                ‘password‘: password,
                ‘client_id‘: client_id,
                ‘client_secret‘: client_secret
            },
            async: false,
            type: ‘post‘,
            success: function (data, textStatus, jqXHR) {
                if (data !== null && data !== ‘‘) {
                    sessionStorage.setItem("access_token", data.access_token);
                    sessionStorage.setItem("expires_in", data.expires_in);
                }
            },
            contentType: ‘application/x-www-form-urlencoded;charset=UTF-8‘,
            headers: {
                Accept: ‘text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8‘
            }
        });
    };

    /**
 * 客戶端
 */
    AuthHttpClient.prototype.getClientToken = function (url, client_id, client_secret, callback, errorCallback, statusCodeCallback) {
        $.ajax(url + ‘token‘, {
            data: {
                ‘grant_type‘: ‘client_credentials‘,
                ‘client_id‘: client_id,
                ‘client_secret‘: client_secret
            },
            type: ‘POST‘,
            success: function (data, textStatus, jqXHR) {
                if (data !== null && data !== ‘‘) {
                    sessionStorage.setItem("access_token", data.access_token);
                    sessionStorage.setItem("expires_in", data.expires_in);
                }
                if (callback !== null) {
                    callback(data);
                }
            },
            error: function (xhr, ajaxOptions, thrown) {
                if (errorCallback !== null) {
                    errorCallback(xhr, ajaxOptions, thrown);
                }
            },
            statusCode: statusCodeCallback,
            contentType: ‘application/x-www-form-urlencoded; charset=UTF-8‘,
            headers: {
                Accept: ‘text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8‘
            }
        });
    };

    /**
    * Get Token
    */
    AuthHttpClient.prototype.getToken = function (url,grant_type,client_id, client_secret, callback, errorCallback, statusCodeCallback) {
        $.ajax(url + ‘token‘, {
            data: {
                ‘grant_type‘: ‘authorization_code‘,
                ‘client_id‘: client_id,
                ‘client_secret‘: client_secret,
                ‘code‘: new AuthHttpClient().request(‘code‘),
                ‘redirect_uri‘: sessionStorage.getItem(‘registered_redirect_uri‘)
            },
            beforeSend: function (request) {
                request.setRequestHeader("Authorization", ‘Basic ‘ +new AuthHttpClient().base64_Encode(client_id + ‘:‘ + client_secret));
            },
            async: false,
            type: ‘post‘,
            success: function (data, textStatus, jqXHR) {
                if (data !== null && data !== ‘‘) {
                    sessionStorage.setItem("access_token", data.access_token);
                    sessionStorage.setItem("expires_in", data.expires_in);
                }
                if (callback !== null) {
                    callback(data);
                }
            },
            error: function (xhr, ajaxOptions, thrown) {
                if (errorCallback !== null) {
                    
                    errorCallback(xhr, ajaxOptions, thrown);
                }
            },
            statusCode: statusCodeCallback,
            contentType: ‘application/x-www-form-urlencoded;charset=UTF-8‘,
            headers: {
                Accept: ‘text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8‘
            }
        });
    };

  

asp.net webapi+swagger+OAuth2.0