10pm 3am,护士网站,安阳同济男科医院
前言
最早2005年 asp.net 2.0 的时候开始, web 应用程序在处理身份验证和授权有了很多的变化,多了比如手机端,平板等,所以那个时候为了适应这种变化就引入了asp.net membership,但是随着时间的发展一些社交网站或者程序聚集了大量的用户,比如facebook,twitter,qq等,这个时候用户希望能够使用他们在这些社交站点身份来登陆当前网站,这样可以免除注册这些琐碎而又必要的操作,用户也不必记住大量的账户密码。
又随着互联网的发展,越来越多的开发者不只是关注具体业务代码的编写,转变为开始关注应用程序代码的单元测试,这已经是开发者关注的核心。所以在2008年,asp.net 团队引入了 mvc 框架,这样来帮助开发者很方便的构建单元测试,同时开发者希望他们的 membership 系统也能够做到这一点。
基于以上,asp.net identity 应运而生。
identity 要解决的问题
很多开发人员说他们不愿意使用identity,自己实现要方便的多,ok,那么需求来了?以下就是我针对此次任务给你提出来的需求。
身份系统
能够对用户资料(user profile)很方便的扩展
持久化
单元测试
角色机制
要支持基于claims
第三方社交登陆
封装为中间件
nuget包提供
以上,就是我提出来的需求,如果让你来封装这样一个用户身份认证组件,你会不是想到以上的这些功能点,那针对于这些功能点你又会怎么样来设计呢?
下面来看一下 identity 怎么样设计的吧。
getting started
抽丝剥茧,我们先从入口看一下其使用方式。 首先我们打开 startup.cs 文件,然后添加如下代码:
public class startup { public void configureservices(iservicecollection services) { services.adddbcontext<applicationdbcontext>(options => options.usesqlserver(configuration["data:defaultconnection:connectionstring"])); services.addidentity<applicationuser, identityrole>(options => { options.cookies.applicationcookie.authenticationscheme = "applicationcookie"; options.cookies.applicationcookie.cookiename = "interop"; }) .addentityframeworkstores<applicationdbcontext>() .adddefaulttokenproviders(); } public void configure(iapplicationbuilder app) { // 使用了 cookieauthentication 中间件做身份认证 app.useidentity(); } }
在 configureservices 中,先是注册了数据库上下文,然后又 services.addidentity() 我们看一下里面都注册了哪些服务呢?
public static identitybuilder addidentity<tuser, trole>( this iservicecollection services, action<identityoptions> setupaction) where tuser : class where trole : class { // 这个就是被 identity 使用的 services.addauthentication(options => { // this is the default value for externalcookieauthenticationscheme options.signinscheme = new identitycookieoptions().externalcookieauthenticationscheme; }); // 注册 ihttpcontextaccessor ,会用到 services.tryaddsingleton<ihttpcontextaccessor, httpcontextaccessor>(); // identity services services.tryaddsingleton<identitymarkerservice>(); services.tryaddscoped<iuservalidator<tuser>, uservalidator<tuser>>(); services.tryaddscoped<ipasswordvalidator<tuser>, passwordvalidator<tuser>>(); services.tryaddscoped<ipasswordhasher<tuser>, passwordhasher<tuser>>(); services.tryaddscoped<ilookupnormalizer, upperinvariantlookupnormalizer>(); services.tryaddscoped<irolevalidator<trole>, rolevalidator<trole>>(); // 错误描述信息 services.tryaddscoped<identityerrordescriber>(); services.tryaddscoped<isecuritystampvalidator, securitystampvalidator<tuser>>(); //身份当事人工厂 services.tryaddscoped<iuserclaimsprincipalfactory<tuser>, userclaimsprincipalfactory<tuser, trole>>(); //三大对象 services.tryaddscoped<usermanager<tuser>, usermanager<tuser>>(); services.tryaddscoped<signinmanager<tuser>, signinmanager<tuser>>(); services.tryaddscoped<rolemanager<trole>, rolemanager<trole>>(); if (setupaction != null) { services.configure(setupaction); } return new identitybuilder(typeof(tuser), typeof(trole), services); }
看了以上代码后,基本上知道了 identity 他的设计的一个架构了,通过此结构我们也能够学习到我们自己封装一个中间件的时候,该怎么样来组织我们的代码结构,怎么样的利用 asp.net core 给我们提供的依赖注入来更好的解耦,下面我们来看一下通过以上代码我们能够学到什么东西:
1、 在 public static identitybuilder addidentity<tuser, trole>(this iservicecollection services, action<identityoptions> setupaction) 这个扩展方法中,提供了一个参数 action<identityoptions>,这个是干什么用的呢? 这个是我们在设计一个中间件的时候,有需要外部提供的参数,就会设计一个 options 的类用来接受外部的参数,然后封装为一个action委托的方式提供。在使用到的地方就可以以 ioption xxx 的形式注入进来使用了。
2、services.tryaddscoped<interface,implement>(),这个注册方式就是说,如果检测到di容器里面已经有了当前要注册的interface或者service,就不会再次注册,没有才会注册进去。 那么为什么此处要这样用呢? 这是因为如果用户已经实现了此接口并且已经注册的容器当中的话,就使用用户注册的,而不是中间件自身的。用户就能很好的对中间件提供的功能进行自定义了,这就是oo中的多态性,这就是里氏替换原则。
3、如果你能理解第2条的话,那么你应该知道为什么会在服务中注册 错误描述信息 那个identityerrordescriber 了吧,也能够解决你想提示账号密码错误,无奈identity输出是英文问题的提示。
4、三大对象,这个是 identity 的核心了,所以学习 identity 的话,在看完博客 asp.net core 之 identity 入门(一,二)之后,学这三个对象就够了。
有些同学可能很好奇,都没有依赖具体的数据库或者是ef,是怎么样做到的增删改查的呢?
这个时候,就需要几个 store 接口派上用场了。以下是identity中定义的store接口:
有了这些接口之后,是不是豁然开朗了,原来 identity 是通过这种方式实现的持久化机制,依赖抽象接口而不是依赖具体的细节实现,这就是面向对象中的依赖倒置原则呀。
identity 和 entityframework
identity 和 entityframework的关系,相信上个章节看懂了之后,就很容易明白了,对的,ef 只是针对于上述 store 接口的实现,不信你看截图的源码:
identity 打头的那些类文件都是定义的需要持久化的entity对象,store结尾的那些就是接口的实现啦。
第三方的 identity 实现
除了 ef 是官方默认提供的持久化库之外,还有一些第三方的库,当然你也可以自己使用 ado.net 或者 drapper 实现。
mangodb 针对于 identity 提供的实现: https://github.com/tugberkugurlu/aspnetcore.identity.mongodb
linqtodb 针对于 identity 提供的实现:https://github.com/linq2db/linqtodb.identity
总结
这篇博文写了蛮久的时间的,一方面是因为在构思怎么样的思路来让大家更好的理解,而不仅仅是使用。因为有太多的文章介绍identity 的使用方式以及代码了,但是最后大家还是不会用。后来想到如果让别人想要理解你的库也好代码也好,让其知道诞生的背景是很重要的,因为这才是设计的初衷。另一方面是因为connect() 2016 大会上,.net core 发布了 1.1 版本,除了把项目升级到1.1之外,也在学习1.1新的一些东西,以便更好给大家分享。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论