加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

ASP.NET Core Authentication认证实现要领

发布时间:2020-08-22 05:36:07 所属栏目:编程 来源:网络整理
导读:这篇文章首要先容了ASP.NET Core Authentication认证实现要领,文中通过示例代码先容的很是具体,对各人的进修可能事变具有必然的参考进修代价,必要的伴侣们下

  在继承往下之前,我们先看一下这个认证中间件的浸染功效,当认证通过期,在HttpContext的User属性(ClaimPrincipal)赋予身份标识,以是在后续的哀求管道中都是基于认证功效中的身份标识做鉴权,这个我们会在后头的现实操纵中会提到。

  言归正传,在这里引出了我们的两个工具AuthenticationHandlerProvider,AuthenticationSchemeProvider。

  重要工具讲授

  IAuthenticationSchemeProvider

  从名字来看,IAuthenticationSchemeProvider的浸染应该是提供Scheme的,这也是Provider在微软的气魄威风凛凛内里起的浸染(相同于工场模式)。

  这个Scheme是什么呢?很明明,在Framework期间,也是有基于差异Scheme验证的,好比Bearer,Cookie,在Aspnet Core中界说差异的Scheme代表着差异的认证处理赏罚方法,详细浮现是在每个Scheme中包括对应的IAuthenticationHandler范例的Handler,由它来完成跟自身Scheme相干的认证处理赏罚。假如没有界说会怎么样?细心看上面这块源码,只有当AuthenticationScheme不为空时才会做认证,不然一旦在Controller打上鉴权标签[Authorize],将会直接返回401,以是我们必需指定本身的Scheme。

  那么我们在那边指定我们的Scheme相同呢?我们先返回到ConfigureService的AddJwtBearer,行使过的伴侣们必定知道,这里获取的Scheme是我们在ConfigureService通过Addxxx scheme指定的Scheme范例。这里我们是行使JWT的

  在这里指定了TOptions 为JwtBearerOptions,而THandler为JwtBearerHandler。

public virtual AuthenticationBuilder AddScheme<TOptions, THandler>( string authenticationScheme, string displayName, Action<TOptions> configureOptions) where TOptions : AuthenticationSchemeOptions, new() where THandler : AuthenticationHandler<TOptions> { return this.AddSchemeHelper<TOptions, THandler>(authenticationScheme, displayName, configureOptions); } private AuthenticationBuilder AddSchemeHelper<TOptions, THandler>( string authenticationScheme, string displayName, Action<TOptions> configureOptions) where TOptions : class, new() where THandler : class, IAuthenticationHandler { this.Services.Configure<AuthenticationOptions>((Action<AuthenticationOptions>) (o => o.AddScheme(authenticationScheme, (Action<AuthenticationSchemeBuilder>) (scheme => { scheme.HandlerType = typeof (THandler); scheme.DisplayName = displayName; })))); if (configureOptions != null) this.Services.Configure<TOptions>(authenticationScheme, configureOptions); this.Services.AddTransient<THandler>(); return this; }

  留意这里TOptions 是必要担任AuthenticationSchemeOptions的,在这里是JwtBearerOptions,而THandler是AuthenticationHandler<TOptions>范例的Handler,在这里是JwtBearerHandler。

  我们回到Scheme的说明继承往下,起首看一下AuthenticationScheme的界说  

public class AuthenticationScheme { /// <summary>Constructor.</summary> public AuthenticationScheme(string name, string displayName, Type handlerType) { if (name == null) throw new ArgumentNullException(nameof (name)); if (handlerType == (Type) null) throw new ArgumentNullException(nameof (handlerType)); if (!typeof (IAuthenticationHandler).IsAssignableFrom(handlerType)) throw new ArgumentException("handlerType must implement IAuthenticationHandler."); this.Name = name; this.HandlerType = handlerType; this.DisplayName = displayName; } /// <summary>The name of the authentication scheme.</summary> public string Name { get; } /// <summary> /// The display name for the scheme. Null is valid and used for non user facing schemes. /// </summary> public string DisplayName { get; } /// <summary> /// The <see cref="T:Microsoft.AspNetCore.Authentication.IAuthenticationHandler" /> type that handles this scheme. /// </summary> public Type HandlerType { get; } }

  在这里可以看到,假如要行使Aspnet Core自身的认证系统,需先注册Scheme,而且该Scheme必需指定一个范例为IAuthenticationHandler的Handler,不然会抛出非常。(这个着实在AddxxxScheme的时辰已经指定了AuthenticationHandler)

  我们再看一下IAuthenticationSchemeProvider的GetRequestHandlerSchemesAsync要领做了什么

  public virtual Task<IEnumerable<AuthenticationScheme>> GetRequestHandlerSchemesAsync() { return Task.FromResult<IEnumerable<AuthenticationScheme>>((IEnumerable<AuthenticationScheme>) this._requestHandlers); }

  这对象返回了_requestHandlers,这是什么?看代码

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读