1、该连接链接到api中基本的swagge功能:http://www.cnblogs.com/hhhh2010/p/5234016.html

2.在swagger中使用验证(这里使用密码验证模式)http://www.cnblogs.com/WJ--NET/p/7195124.html   <---搭建自己的web api验证服务器

3.在项目中会自动生成SwaggerConfig和Startup文档,如果是有验证服务器的话,swagger的配置就不需要在swaggerconfi中配置了,直接在Startup中配置swagger;

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/3329b4126886

Swagger中添加Token验证的更多相关文章

  1. 用Retrofit发送请求中添加身份验证

    用Retrofit发送请求中添加身份验证====================在安卓应用开发中, retrofit可以极大的方便发送http网络请求,不管是GET, POST, 还是PUT, DEL ...

  2. webapi中使用token验证(JWT验证)

    本文介绍如何在webapi中使用JWT验证 准备 安装JWT安装包 System.IdentityModel.Tokens.Jwt 你的前端api登录请求的方法,参考 axios.get(" ...

  3. Django中csrf token验证原理

    我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...

  4. c# asp.net 中使用token验证

    基于token的鉴权机制类似于http协议也是无状态的,它不需要在服务端去保留用户的认证信息或者会话信息.这就意味着基于token认证机制的应用不需要去考虑用户在哪一台服务器登录了,这就为应用的扩展提 ...

  5. swagger请求参数在header中添加token

    网友大部分说的是如下配置 参照配置然而没有作用 注掉改红框内的配置,在方法上加如下注释就可以用 @ApiImplicitParams({ @ApiImplicitParam(paramType = & ...

  6. Vue中实现token验证

    前后端流程分析 前端页面进行登录操作,将用户名和密码发给服务器 服务器进行校验,通过后生成token,包含信息有密匙.uid.过期时间等,然后返回给前端 前端将token保存在本地(建议在localS ...

  7. adding validation annotators to model classes 在linq to EntityFrame的Model中添加前台验证validation annotators

    The same solution can be applied for LINQ to SQL. The snippet the article shows for using the Metada ...

  8. Asp.Net Core 3.1 学习3、Web Api 中基于JWT的token验证及Swagger使用

    1.初始JWT 1.1.JWT原理 JWT(JSON Web Token)是目前最流行的跨域身份验证解决方案,他的优势就在于服务器不用存token便于分布式开发,给APP提供数据用于前后端分离的项目. ...

  9. [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则

    目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...

随机推荐

  1. R 连接DB2数据库,并制作词图

    #写在前面的话:此教程主要是用R连接了DB2数据库,并进行文本分析,制作了词图 #教程为markdown编写 ---title: "网站留言分析"output: html_docu ...

  2. svn SSL 错误:Key usage violation in certificate has been detected

    CentOS/RHEL yum 安装的 subversion 是 1.6.11 版本,连VisualSVN服务器时会有"Key usage violation"的错误 将subve ...

  3. 【从零开始】【Java】【1】Git和svn

    闲聊 干活快一年了吧,感觉工作中能干的事情也有一点了,但总有种不通透的感觉,查一个问题,能一路查出一堆不明白的东西. 之前新建过文档是记录点点滴滴的知识的,使用上没问题了,但原理什么的还是不懂,想了想 ...

  4. 通过nvm 切换 npm 版本

    通过 nvm-windows 更新 npm 1.先安装 nvm-windows .成功后可在命令窗口 输入nvm 查看到 nvm的版本号. 2..在命令窗口输入 nvm list,查看当前使用的 no ...

  5. sqlserver重组索引,优化碎片

    dbcc dbreindex('digitlab.dbo.RequestForm','',90) dbcc dbreindex('digitlab.dbo.Requestitem','',90) db ...

  6. day25-2 random,os,sys模块

    目录 random 为什么要有random模块,random模块有什么用 os 为什么要有os模块,os模块有什么用 sys 为什么要有sys模块,sys模块有什么用 random import ra ...

  7. [luogu2585 ZJOI2006] 三色二叉树 (树形dp)

    传送门 Description Input 输入文件名:TRO.IN 输入文件仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件名:TRO.OUT 输出文件也只有一行,包 ...

  8. redis_ 5 集群

    [转自 ]https://www.cnblogs.com/hjwublog/p/5681700.html#_label0 Redis集群简介 Redis 集群是3.0之后才引入的,在3.0之前,使用哨 ...

  9. hdu 2435dinic算法模板+最小割性质

    hdu2435最大流最小割 2014-03-22 我来说两句 来源:hdu2435最大流最小割 收藏 我要投稿 2435 There is a war 题意: 给你一个有向图,其中可以有一条边是无敌的 ...

  10. ES scroll(ES游标) 解决深分页

    ES scroll(ES游标) 解决深分页. Why 当Elasticsearch响应请求时,它必须确定docs的顺序,排列响应结果.如果请求的页数较少(假设每页20个docs), Elasticse ...