设为首页 加入收藏

TOP

AspNet Katana中Authentication有关的业务逻辑(一)
2019-09-25 11:18:22 】 浏览:95
Tags:AspNet Katana Authentication 有关 业务 逻辑

我将从CookieAuthenticationMiddleware中间件的使用,来讲述cookie认证是如何实现的

1、系统是如何调用CookieAuthenticationMiddleware的

在web.config的appSettings里添加<add key="owin:AppStartup" value="你自己的config类:namespace.class" >

public class IdentityConfig
{
    public void Configuration(IAppBuilder app)
    {
       // 默认使用CookieAuthenticationMiddleware
       app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });
    }
}

在CookieAuthenticationExtensions里定义了默认的cookieAuthentication中间件

public static IAppBuilder UseCookieAuthentication(this IAppBuilder app, CookieAuthenticationOptions options, PipelineStage stage)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            // 默认中间件
            app.Use(typeof(CookieAuthenticationMiddleware), app, options);
            app.UseStageMarker(stage);
            return app;
        }

2、主要class说明

CookieAuthenticationMiddleware:项目使用时调用的验证中间件类,下面简称 【cookieAuth中间件类】

AuthenticationMiddleware<CookieAuthenticationOptions>:CookieAuthenticationMiddleware的父类,下面简称 【Auth中间件类】

---下面说的是重点,实际的工作处理者--------

CookieAuthenticationHandler:【cookieAuth处理类】

AuthenticationHandler<TOptions>:泛型抽象类,主要有个方法Initialize。是CookieAuthenticationHandler的父类,下面简称 【Auth处理子类】。

AuthenticationHandler:抽象类,主要有BaseInitializeAsync和TeardownAsync方法,是AuthenticationHandler<TOptions>的父类,下面简称 【Auth处理基类】。

3、代码功能说明

public abstract class AuthenticationMiddleware<TOptions> : OwinMiddleware where TOptions : AuthenticationOptions
{
    protected AuthenticationMiddleware(OwinMiddleware next, TOptions options)  : base(next)
    {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            Options = options;
    }

    public TOptions Options { get; set; }

    // 具体的执行流程很简单,分为创建,初始化,下一个中间件执行,卸载
    public override async Task Invoke(IOwinContext context)
    {
        //获取处理者,【cookieAuth处理类】
        AuthenticationHandler<TOptions> handler = CreateHandler();
        //初始化,会调用【Auth处理基类】的BaseInitializeAsync,具体查看---Initialize说明---
        await handler.Initialize(Options, context);
        if (!await handler.InvokeAsync())//默认返回false
        {
            //调用下一个中间件,比方说调用MVC中间件
            await Next.Invoke(context);
        }
        // 最后执行,会调用【Auth处理基类】的TeardownAsync,具体说明查看---Teardown说明---
        await handler.TeardownAsync();
    }

    protected abstract AuthenticationHandler<TOptions> CreateHandler();
}

---Initialize说明---
初始化的时候,将获取已有的ticket,供后续的中间件使用
将调用【Auth处理基类】的BaseInitializeAsync来完成初始化

 protected async Task BaseInitializeAsync(AuthenticationOptions options, IOwinContext context)
        {
            _baseOptions = options;
            Context = context;
            Helper = new SecurityHelper(context);
            RequestPathBase = Request.PathBase;

            _registration = Request.RegisterAuthenticationHandler(this);
           // 设置响应事件,在teardown之后会执行
            Response.OnSendingHeaders(OnSendingHeaderCallback, this);

            await InitializeCoreAsync();
            // 主动模式时执行
            if (BaseOptions.AuthenticationMode == AuthenticationMode.Active)
            {
                // 根据cookie得到ticket,判断是否需要renew,后续的中间件可以获取identity信息
                AuthenticationTicket ticket = await AuthenticateAsync();
                if (ticket != null && ticket.Identity != null)
                {
                    // 将identity添加到context.Request.User里
                    Helper.AddUserIdentity(ticket.Identity);
                }
            }
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇开箱即用简单便捷的轻量级开源开.. 下一篇基于Asp.Net Core MVC和AdminLTE..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目