翻译:WebApi 认证--用户认证Oauth解析

摘要:
使用IAuthenticationManager接口在OwinContext中处理认证的属性,方法如:SignIn,SignOut,AuthenticateAsync或Challenge。Onebuilt-inmechanismthatusestheauthenticationmanageristhenewHostAuthenticationFilterinWebAPIv2–willcometothatlater.Let’sfirsthavealookwhichauthenticationmiddlewaregetsactuallywiredup.一个内建的机制使用认证管理器,在WebAPIV2中是newHostAuthenticationFilter。我们首先看认证中间件是如何连接的。app.UseCookieAuthentication;Thiscalladdssupportsforclassiccookiebasedauthentication.TheauthenticationtypeissimplycalledCookiesorincodethemiddlewareisreferencedusingCookieAuthenticationDefaults.AuthenticationType.这个调用将增加对基于经典Cookie认证的支持。认证类型简单的被称之为Cookies或这在中间件中使用usingCookieAuthenticationDefaults.AuthenticationType.app.UseExternalSignInCookie;ThesecondcookiemiddlewareregistersitselfasExternalCookie.Thiscookieisusedtotemporarilystoreinformationaboutauserlogginginwithathirdpartyloginprovider这个Cookie中间件将其自身作为一个ExternalCookie注册到系统中。这个Cookie被用在为使用第三方登录提供方的临时存储用户登录信息。

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,AuthenticateAsyncChallenge。每个方法需要哦一个认证类型作为指示其如何具体工作。

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:

这个扩展方法实际上注册了三个中间件:

  1. 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 的逻辑,在下篇中我们会详细的探讨。

  1. 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(声明)

  1. 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解析》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇跟着我一步一步的搭建一个基于springcloud的微服务实例使用 IDEA和Maven 整合SSH框架下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何运用PHP+REDIS解决负载均衡后的session共享问题

一、为什么要使用Session共享? 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名、密码在整个网站的各个模块中都是可以登录使用的。各个服务器共享用户数据是比较容易实现的,只需要在后端放个数据库服务器,各个服务器通过统一接口对用户数据进行访问即可。但还存在一个...

asp.net webapi 返回json结果的方法

第一种: public static void Register(HttpConfiguration config) { //1、将默认的xml格式化程序清除 GlobalConfiguration.Configuration.Formatters.XmlFormatter.Supporte...

Nginx负载均衡会话共享

    在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决 1.使用nginx的ip_hash,根据客户端的来源IP,将请求分配到相同服务器上 2.基于服务端的Session会话共享(mysql/memcache/redis/file)   在解决负载均衡会话问题我们需要了解session和cookie。 1.用户第一次请求服务端网站时,服务...

在.net core 的webapi项目中将对象序列化成json

问题:vs2017 15.7.6创建一个基于.net core 2.1的webapi项目,默认生成的控制器继承自ControllerBase类         在此情况下无法使用Json()方法 将一个对象转成json  解决方案:将控制器继承的类 改为Controller 此时可以使用json()方法 将对象直接转成json。...

cookie的生成,读取cookie,删除cookie

一般在做登录和页面的用户认证时,我们的都会选择用cookie来传值,为啥,因为cookie前后端都可以获取,多好啊,那么前端用js对cookie的设置,就分享一下吧 JS设置cookie: 假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为: document.cookie="name="+...

loadrunner---<二>---菜鸟对cookie的思考

http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟对cookie的思考 lr是怎么将cookie添加到录制的脚本中的?lr中cookie是做什么的? 首先将解决两个疑问: 1--什么是cookie?-- C...