aspnetcore 认证相关类简要说明一
首先我想要简要说明是AuthenticationScheme类,每次看到Scheme这个单词我就感觉它是一个很高大上的单词,其实简单翻译过来就是认证方案的意思。既然一种方案,那我们就要知道这个方案的名字(Name)和它对外宣传的名字(DisplayName)以及这方案的认证处理类型(Type handlerType)。
namespace Microsoft.AspNetCore.Authentication
{
public class AuthenticationScheme
{
public AuthenticationScheme(string name, string displayName, Type handlerType);
//The name of the authentication scheme.
public string Name { get; }
//The display name for the scheme. Null is valid and used for non user facing schemes.
public string DisplayName { get; }
//The Microsoft.AspNetCore.Authentication.IAuthenticationHandler type that handles this scheme.
public Type HandlerType { get; }
}
}
aspnetcore同时还提供了一个构建AuthenticationScheme类的AuthenticationSchemeBuilder类。AuthenticationSchemeBuilder的属性和AuthenticationScheme是一样的,这些属性将作为实例化AuthenticationScheme类的参数。AuthenticationSchemeBuilder仅比AuthenticationScheme多了一个Build方法。
namespace Microsoft.AspNetCore.Authentication
{
/// <summary>
/// Used to build <see cref="AuthenticationScheme"/>s.
/// </summary>
public class AuthenticationSchemeBuilder
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="name">The name of the scheme being built.</param>
public AuthenticationSchemeBuilder(string name)
{
Name = name;
}
...
//一些和AuthenticationScheme类相同的属性,将作为构建AuthenticationScheme的参数
...
// Builds the <see cref="AuthenticationScheme"/> instance.
public AuthenticationScheme Build() => new AuthenticationScheme(Name, DisplayName, HandlerType);
}
}
而AuthenticationSchemeBuilder又在那里注册进来的呢?接下来我们看另外一个类:AuthenticationOptions
public class AuthenticationOptions
{
public AuthenticationOptions();
public IEnumerable<AuthenticationSchemeBuilder> Schemes { get; }
public IDictionary<string, AuthenticationSchemeBuilder> SchemeMap { get; }
public string DefaultScheme { get; set; }
public string DefaultAuthenticateScheme { get; set; }
public string DefaultSignInScheme { get; set; }
public string DefaultSignOutScheme { get; set; }
public string DefaultChallengeScheme { get; set; }
public string DefaultForbidScheme { get; set; }
public void AddScheme(string name, Action<AuthenticationSchemeBuilder> configureBuilder);
public void AddScheme<THandler>(string name, string displayName) where THandler : IAuthenticationHandler;
}
}
该类的主要作用就是让我们配置AuthenticationScheme的一些选项。
我们可以通过该类的AddScheme和AddScheme<THandler>方法添加方案到AuthenticationOptions类Schemes和SchemeMap属性中。属性SchemeMap是一个字典类型,它是以我们AuthenticationScheme的Name属性作为Key。aspnetcore默认提供了几个方案就不一 一列具了,还有一点我们需要注意的是AddScheme<THandler>中THandler必须是IAuthenticationHandler接口类型。我们可以通过在Startup类的ConfigureServices方法中注入我们的方案:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(x =>
{
x.AddScheme<MyAuth>("abc", "我的中国方案");
});
}
MyAuth是我自己实现IAuthenticationHandler接口添加的类,认证相关的逻辑就实现在他们的方法中,代码如下:
public class MyAuth : IAuthenticationHandler
{
public Task<AuthenticateResult> AuthenticateAsync()
{
throw new NotImplementedException();
} public Task ChallengeAsync(AuthenticationProperties properties)
{
throw new NotImplementedException();
} public Task ForbidAsync(AuthenticationProperties properties)
{
throw new NotImplementedException();
} public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
throw new NotImplementedException();
}
}
通过以上的短篇小论,我们主要梳理了AuthenticationScheme、AuthenticationSchemeBuilder和AuthenticationOptions三个类。
aspnetcore 认证相关类简要说明一的更多相关文章
- aspnetcore 认证相关类简要说明二
能过<aspnetcore 认证相关类简要说明一>我们已经了解如何将AuthenticationOptions注入到我们依赖注入系统.接下来,我们将了解一下IAuthenticationS ...
- aspnetcore 认证相关类简要说明三
今天我们再来了解一个很重要的接口IAuthenticationService的实现类AuthenticationService: public class AuthenticationService ...
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- 21 BasicTaskScheduler基本任务调度器(一)——Live555源码阅读(一)任务调度相关类
21_BasicTaskScheduler基本任务调度器(一)——Live555源码阅读(一)任务调度相关类 BasicTaskScheduler基本任务调度器 BasicTaskScheduler基 ...
- 8 延时队列相关类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- 4 Handler相关类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. Handler相关类概述 处理程序相关类一共有三个,其没有派生继承关系,但是其有友元关系和使用关系 ...
- MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)
前面讲了模态对话框和非模态对话框,本节来将一种特殊的对话框--属性页对话框. 属性页对话框的分类 属性页对话框想必大家并不陌生,XP系统中桌面右键点属性,弹出的就是属性页对话框,它通过标签切换各个页面 ...
- android 6.0 SDK中删除HttpClient的相关类的解决方法
一.出现的情况 在eclipse或 android studio开发, 设置android SDK的编译版本为23时,且使用了httpClient相关类的库项目:如android-async-http ...
- Android 6.0删除Apache HttpClient相关类的解决方法
相应的官方文档如下: 上面文档的大致意思是,在Android 6.0(API 23)中,Google已经移除了Apache HttpClient相关的类,推荐使用HttpUrlConnection. ...
随机推荐
- res/raw与assets目录的区别
1.相同点: 两者都会原封不动的保存在apk包中,不会被编译成二进制码. 2.不同点: raw目录下只能存放文件,不能存放下一级的文件夹,而assets可以存放下一级的文件夹. raw目录下的资源会映 ...
- python-pymongo使用
#-*- coding: utf-8 -*- #python2.7x from pymongo import MongoClient def get_db(): #建立连接 client = Mong ...
- JVM执行篇:使用HSDIS插件分析JVM代码执行细节--转
http://www.kuqin.com/java/20111031/314144.html 在<Java虚拟机规范>之中,详细描述了虚拟机指令集中每条指令的执行过程.执行前后对操作数栈. ...
- 有线mac 无线MAC 地址&&telnet要先开服务: ipconfig /all getma
在向IEEE申请到Mac address后,写到Lan设备里,就是Lan Mac,写到Wlan设备里,就是wlan Mac,写到BT设备里,就是BT Mac. MAC(Media Access Con ...
- String.replace与String.format
字符串的替换函数replace平常使用的频率非常高,format函数通常用来填补占位符.下面简单总结一下这两个函数的用法. 一.String.replace的两种用法 replace的用法如:repl ...
- c# 删除文件,清理删除文件
c# 删除程序占用的文件,清理删除文件,彻底删除文件,解除文件占用 文件打开时,以共享读写模式打开 FileStream inputStream = new FileStream(name, File ...
- String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace
String.IsNullOrEmpty 指示指定的字符串是否为 null 或者 空字符串: 返回值:如果参数为 null 或者 空字符串("" .String.Empty),结果 ...
- [Linux]C语言Linux系统编程创建进程
1.进程ID 每一个进程都由一个唯一的标识符表示,即进程ID,简称pid.系统保证在某时刻每个pid都是唯一的. 1.1分配进程ID 缺省情况下,内核将进程ID的最大值限制为32768,可以在此处设置 ...
- 撩课-Java每天10道面试题第4天
撩课Java+系统架构 视频 点击开始学习 31.静态变量和实例变量的区别? 静态变量也叫类变量, 这种变量前加了static修饰符. 可以直接用类名调用, 也可以用对象调用, 而且所有对象的同一个类 ...
- GString惰性求值
当对一个GString实例求值时,如果其中包含一个变量,该变量的值会被简单地打印到一个Writer,通常是一个StringWriter.然而,如果GString中包含的是一个闭包,而非变量,该闭包就会 ...