翻译:WebApi 认证--用户认证Oauth解析
The Web API v2用户认证模板提供了流行的应用用户认证场景,如.使用本地帐号的用户名密码认账 (包括创建用户、设置和修改密码)以及使用第三方的认证方式,如facebook,google等等– 在本地中包含了外部帐号的连接 所有的这些均通过使用一个OAuth2认证服务进行.
To make all that happen the template combines quite a bit of new stuff together: OWIN, Katana authentication middleware, ASP.NET identity, OAuth2 and a bunch of new authentication related attributes…and I must admit figuring out exactly what’s going on was a bit of a challenge. Two quotes constantly came to mind while digging through the source code and writing down my notes. One was: complexity is the natural enemy of security – and the other one was: shit’s hard. So enjoy.
为了实现这些,模板集合了一些技术:OWIN、Katana认证中间件、ASP.NET 标识、OAuth2以及一些新的与认证相关的特性…,必须指出这是一个挑战.
In this post I want to focus on the general setup of the Katana authentication middleware, the following posts will deal with the local account features and the external authentication.
In Katana, every authentication middleware “registers” itself with the system. For that it needs a “name” – or technically speaking an AuthenticationType. Using that name, some code like a framework can call into the authentication component. This is done using theIAuthenticationManager interface which hangs off the Authentication property on theOwinContext. It features methods like SignIn, SignOut, AuthenticateAsync or Challenge. Each of these methods require an AuthenticationType as a hint which middleware will do the actual work.
在Katana中,每个认证中间件将自己注册到系统中,因此它需要一个名字,或从技术上称之为一个认证类型。使用这个名字,代码会想一个框架一样可以调用认证组件。使用IAuthenticationManager 接口在OwinContext中处理认证的属性,方法如:SignIn,SignOut,AuthenticateAsync 或Challenge。每个方法需要哦一个认证类型作为指示其如何具体工作。
One built-in mechanism that uses the authentication manager is the newHostAuthenticationFilter in Web API v2 – will come to that later. Let’s first have a look which authentication middleware gets actually wired up (see also Startup.Auth.cs).
一个内建的机制使用认证管理器,在Web API V2中是newHostAuthenticationFilter。我们首先看认证中间件是如何连接的(参照Startup.Auth.cs)。
For the implicit flow and the interaction with Google and friends, “browser tech” is needed (think web views in native apps, or the browser itself for JS) – this is where cookies come in:
在隐式流程和与Google和朋友互动时,需要前端技术的支持(考虑本地App的Web页面或者浏览器的JS),即就是使用Cookie。
app.UseCookieAuthentication(new CookieAuthenticationOptions());
This call adds supports for classic cookie based authentication. The authentication type is simply called Cookies or in code the middleware is referenced usingCookieAuthenticationDefaults.AuthenticationType.
这个调用将增加对基于经典Cookie认证的支持。认证类型简单的被称之为Cookies或这在中间件中使用usingCookieAuthenticationDefaults.AuthenticationType.
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
The second cookie middleware registers itself as ExternalCookie (or DefaultAuthenticationTypes.ExternalCookie). This cookie is used to temporarily store information about a user logging in with a third party login provider
这个Cookie中间件将其自身作为一个ExternalCookie(或DefaultAuthenticationTypes.ExternalCookie)注册到系统中。这个Cookie被用在为使用第三方登录提供方的临时存储用户登录信息。
Further there is one authentication middleware registered for every external login provider you want to support (authentication types: Google, Facebook, Twitter and Microsoft):
你需要为想支持的每个外部登录提供方注册一个认证中间件(认证类型:Google、Facebook、Twitter以及微软):
app.UseFacebookAuthentication(appId: “178…455″,appSecret: “f43…f”);
app.UseGoogleAuthentication();
OK – next up is all the plumbing to support token-based authentication – we need a token producer and consumer. This is all hidden behind the following line of code:
好了,接下来是对基于令牌认证的支持,我们需要一个令牌生产者和消费者。这些都会被隐藏在下面代码的中执行:
app.UseOAuthBearerTokens(OAuthOptions);
This extension method actually registers three middlewares behind the covers:
这个扩展方法实际上注册了三个中间件:
- OAuth2 authorization server to deal with resource owner flow and implicit flow token requests. Application specific logic is encapsulated in the ApplicationOAuthProviderclass which we’ll have a closer look in the next post.
OAuth2认证服务器来处理资源所有者流程以及隐式令牌获取流程。应用程序指定包装ApplicationOAuthProviderclass 的逻辑,在下篇中我们会详细的探讨。
- Token-based authentication for local accounts using an authentication type of Bearer(or OAuthDefault.AuthenticationType). This middleware only accepts claims where the issuer has been set to LOCAL AUTHORITY.
为本地帐号进行基于令牌的认证使用一种认证类型为Bearer的认证(或OAuthDefault.AuthenticationType)。这个中间件只接受发行人被设置为本地权限的claims(声明)
- Token-based authentication for external accounts (resulting from an authentication handshake with an external login provider). It uses an authentication type ofExternalBearer (or DefaultAuthenticationTypes.ExternalBearer) and only accepts claims where the issuer is not LOCAL AUTHORITY (important technical detail – keep that in the back of your mind).
对外部账户进行的基于令牌的认证(结果来自一个与外部登录提供者的认证握手)。他使用认证类型为ofExternalBearer (或DefaultAuthenticationTypes.ExternalBearer)并只接受发行人不是本地认证的声明(重要的技术细节)
With that setup you can now control which authentication type is required to access which parts of the API surface – let me give you some examples:
有了这些步骤,现在你便可以控制需要的认证类型来访问API,例如
In general Web API requires token-based authentication using local accounts (Bearer). This is why you find the following two lines of code in WebApiConfig.cs:
一般WebApi需要使用本地帐号(Bearer)基于令牌的认证。这是为什么需要在WebApiConfig.cs 文件中找到下面两行代码:
config.SuppressDefaultHostAuthentication();
config.Filters.Add(
new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Let’s say you’d want to also accept tokens resulting from external authentication – but require an authenticated principal, the following would work (e.g. on a controller or action):
你也可以从外部认证获取令牌,但是需要一个认证原则,下面的代码便可以工作(如一个控制器或动作)
[Authorize]
[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
If you want to override the global setting and only accept an application cookie if present (a technique used in the account controller – more on that in the next post) – you could do this:
如果你想覆盖全局配置并只允许一个应用程序的Cookie,你可以这样做:
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
翻译:WebApi 认证--用户认证Oauth解析的更多相关文章
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_01-用户认证-用户认证流程分析
1 用户认证 1.1 用户认证流程分析 用户认证流程如下: 访问下面的资源需要携带身份令牌和jwt令牌,客户端可以通过身份认证的令牌从服务端拿到长令牌, 一会要实现认证服务请求用户中心从数据库内来查询 ...
- 用户认证授权和Shiro入门
1.权限管理基础(认证和授权): 前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习S ...
- 学成在线(第17天)用户认证 Zuul
用户认证 用户认证流程分析 用户认证流程如下: 业务流程说明如下: 1.客户端请求认证服务进行认证.2.认证服务认证通过向浏览器cookie写入token(身份令牌)认证服务请求用户中心查询用户信息. ...
- apache用户认证,ssl双向认证配置
安装环境: OS:contos 6.4 httpd:httpd-2.2.15-59.el6.centos.i686.rpm openssl:openssl-1.0.1e-57.el6.i686.rpm ...
- Apache用户认证、域名跳转、Apache访问日志
5月29日任务 课程内容: 11.18 Apache用户认证11.19/11.20 域名跳转11.21 Apache访问日志扩展 apache虚拟主机开启php的短标签 http://ask.apel ...
- Nginx网站用户认证
一.Nginx网站用户认证 用户认证:用户访问网页时需要输入一个用户名和密码才能打开网页. nginx的默认网页时安装目录下的html/index.html,配置文件在安装目录下的conf目录中的ng ...
- 5分钟搞懂:基于token的用户认证
https://www.qikegu.com/easy-understanding/880 用户认证 用户认证或者说用户登录是确认某人确实是某人的过程,生活中靠身份证,网络上就要靠账号和密码.用户提供 ...
- Mysql之用户认证授权管理
概述 Mysql的认证采用账号密码方式,其中账号由两个部分组成:Host和User:Host为允许登录的客户端Ip,User为当前登录的用户名. 授权没有采用典型的RBAC(基于角色的访问控制),而是 ...
- 使用Autodesk OAuth服务在用户认证的示例
大家知道以Autodesk 360为核心的Autodesk 云服务已经陆续发布,ReCap API.InfraWorks API和PLM 360 REST API已经开始的Pilot项目供第三方开发者 ...
随机推荐
- Primitive Objects
[Primitive Objects] Unity can work with 3D models of any shape that can be created with modelling so ...
- 支付结果回调v7核心,投保确认接口..
<?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST"&g ...
- QListWidgetItem带上颜色的问题
new_item = QListWidgetItem(_fromUtf8(item_content), self.listWidget) 首先创建一个QListWidgetItem,第一个参数内容是I ...
- 检测iOS的APP性能的一些方法
首先如果遇到应用卡顿或者因为内存占用过多时一般使用Instruments里的来进行检测.但对于复杂情况可能就需要用到子线程监控主线程的方式来了,下面我对这些方法做些介绍: Time Profiler ...
- mssql 用户只能查看授权的数据库
问题背景:公司的一台数据库服务器上放在多个数据库,每个数据库都使用不同的登录名称,但在将项目文件发布到Ftp时,有些Ftp的信息是在客户那边的 一旦客户那边使用配置文件中的数据库信息连接到数据库他就能 ...
- opencv win7 配置
按照官网的配置方法,结果出现了下面的错误信息F:\eclipse C++ workspace\test\Debug/../src/test.cpp:18: undefined reference to ...
- Objc基础学习记录2
1.[类 方法名]; //类方法,-静态成员函数, + (void)fun; 2.[对象名 方法名]; //实例方法, -非静态成员函数, - (void) fun; 3.带有冒号必须要有参数; 4. ...
- 图片懒加载 lazyload
添加引用 <script type="text/javascript" src="lazyload/yahoo-dom-event.js">< ...
- 利用C#实现对excel的写操作
一.COM interop 首先我们要了解下何为COM Interop,它是一种服务,可以使.NET Framework对象能够与COM对象通信.Visual Studio .NET 通过引入面向公共 ...
- 批处理脚本命令行方式关闭Windows服务
对于一些不常用的Windows Services,可以通过设置其启动类型为"禁用"而将其关闭.这种关闭方式是长期性的,电脑重启之后仍然起作用. 有时候希望在批处理脚本里通过命令行方 ...