tnblog
首页
视频
资源
登录
什么时候才能领悟,取之越多失之越多
排名
5
文章
229
粉丝
15
评论
7
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

identity server4身份验证中间件源码

5470人阅读 2020/1/17 10:48 总访问:1179003 评论:0 收藏:0 手机
分类: .net core

通过下载ids4的源码来看,可以把相关的源码放到自己项目中,方便分析整个流程和进行一些个性化定制,下面贴一下身份验证中间件的源码


扩展方法:

  1.  public static class MyAuthAppBuilderExtensions
  2.     {
  3.         public static IApplicationBuilder UseMyAuthentication(this IApplicationBuilder app)
  4.         {
  5.             if (app == null)
  6.             {
  7.                 throw new ArgumentNullException(nameof(app));
  8.             }
  9.             return app.UseMiddleware<MyAuthenticationMiddleware>();
  10.         }
  11.     }

具体中间件的类:

  1. public class MyAuthenticationMiddleware
  2.     {
  3.         private readonly RequestDelegate _next;
  4.         public MyAuthenticationMiddleware(RequestDelegate next, IAuthenticationSchemeProvider schemes)
  5.         {
  6.             if (next == null)
  7.             {
  8.                 throw new ArgumentNullException(nameof(next));
  9.             }
  10.             if (schemes == null)
  11.             {
  12.                 throw new ArgumentNullException(nameof(schemes));
  13.             }
  14.             _next = next;
  15.             Schemes = schemes;
  16.         }
  17.         public IAuthenticationSchemeProvider Schemes { getset; }
  18.         public async Task Invoke(HttpContext context)
  19.         {
  20.             context.Features.Set<IAuthenticationFeature>(new AuthenticationFeature
  21.             {
  22.                 OriginalPath = context.Request.Path,
  23.                 OriginalPathBase = context.Request.PathBase
  24.             });
  25.             // Give any IAuthenticationRequestHandler schemes a chance to handle the request
  26.             var handlers = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
  27.             foreach (var scheme in await Schemes.GetRequestHandlerSchemesAsync())
  28.             {
  29.                 var handler = await handlers.GetHandlerAsync(context, scheme.Name) as IAuthenticationRequestHandler;
  30.                 bool issccuess = await handler.HandleRequestAsync();
  31.                 if (handler != null && issccuess)
  32.                 {
  33.                     return;
  34.                 }
  35.             }
  36.             var defaultAuthenticate = await Schemes.GetDefaultAuthenticateSchemeAsync();
  37.             if (defaultAuthenticate != null)
  38.             {
  39.                 //这里边成功了才能拿到用户信息
  40.                 var result = await context.AuthenticateAsync(defaultAuthenticate.Name);
  41.                 if (result?.Principal != null)
  42.                 {
  43.                     context.User = result.Principal;
  44.                 }
  45.             }
  46.             await _next(context);
  47.         }
  48.     }


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

identity server4 登录成功跳回到signin-oidc得到404

使用identity server4 做单点登录,登录成功后跳转到/signin-oidc得到404,纠结了很久,记录一下。我这里的环境是vs2019和.n...

identity server4 常见错误

登录成功后跳转报错:An error occurred while processing your request可能原因1:登录成功后的跳转地址没有signin-oidc需...

identity server4 实现单点登录

我希望有个如你一般的人我希望有个如你一般的人,如山间清爽的风,如古城温暖的光,从清晨到夜晚,从山野到书房,只要最后...

identity server4 的授权模式

授权模式OAuth2.0 定义了四种授权模式:Implicit:简化模式;直接通过浏览器的链接跳转申请令牌。Client Credentials:客户...

identity server4 四种授权模式

爱情哪有那么复杂,能让你开开心心笑得最甜的那个人就是对的人下面介绍4种模式安全性从低到高客户端模式客户端模式只对客户...

identity server4登录成功后,跳转到原来的页面

我们用identity server4现在访问权限一般都是在某个控制器加上Authorize特性,这样就会访问他的时候跳转到授权中心。这种方...

identity server4身份验证流程分析

当一个项目登录后,另外一个项目直接拿不到用户信息,必须要加上[Authorize]才能获取获取用户信息,所以我们来分析一下加上...

identity server4判断用户是否已经登录

User.Identity.IsAuthenticated实现这句代码就可以判断了

identity server4 cookie相关配置

如果我们这一配置的话在登录后就可以看到cookie信息

.net core 3.1 identity server4 (ClientCredentials模式)

.net core 3.1 Identity Server4 (ClientCredentials模式)[TOC] ClientCredentials 模式的理解 在这之前我先问大家...

.net core 3.1 Ocelot 与 identity server4 鉴权

目录与前言目录链接:.net core Ocelot 简单网关集群熔断架构整合目录.net core 3.1 Identity Server4 (ClientCredentials...

.net core 3.1 identity server4 (Password模式)

.net core 3.1 Identity Server4 (Password模式)[TOC] Password 模式的理解 当应用程序将用户的用户名和密码交换为...

.net core 3.1 identity server4 (Code模式)

.net core 3.1 Identity Server4 (Code模式)[TOC] Code 模式的理解 大致说一下,这种授权模式的意义。A. 用户通...

.net core 3.1 identity server4 (Implicit模式)

.net core 3.1 Identity Server4 (Implicit模式)[TOC] Implicit 模式的理解 A.用户通过浏览器访问客户端,然后客...

.net core 3.1 identity server4 (Hybrid模式)

.net core 3.1 Identity Server4 (Hybrid模式)[TOC] Hybrid 模式的理解 Hybrid 模式相当于(Code模式+Impact模式),所...