zoukankan      html  css  js  c++  java
  • ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括

    ASP.NET Identity 是4.5中引入的,支持Clamis(声明)式样登陆【即认证和授权分开模式】,结合owin可以实现cookie加密等功能。

    1.ASP.NET Identity架构框架说明

      最上面是集成实现中间(identity.entityframwork---它是实现了用户数据的存储方式,这层是微软自己实现的基于EF存储的实现层。可以直接几重identity.core重写以实现不同存储方式)。
      其中IuserStore UserStore是实现对用户对象在存储的一些数据操作方法实现,比如密码验证方法或者查找用户方法等。
      identityUser继承自底层IUser,可以扩展用户的字段数据等。

      最终会把IUserStore作为参数实例化UserManager来做用户的相关业务逻辑操作。

    2、OWIN是微软定义了一套替代IIS管道处理的东西,这样 request和response上下文content的操作和appapliction等操作都托管给了Owin处理。

      结合实现声明式(Claims)登陆为例子解释owin,下面是登陆代码

    // 1. 利用ASP.NET Identity获取用户对象
    var user = await UserManager.FindAsync("UserName", "Password");
    // 2. 利用ASP.NET Identity获取ClaimsIdentity(identity 对象,包含用户的基本信息 )
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    // 3. 将上面拿到的identity对象利用OWIN的管道处理方法登录,加密写入读取coocie和处理 及管理ClaimsPrincipal对象(是2的封装,这个对象是赋值给http--> crrentUser)
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);  

     结合上面和下面图片代码(引用园友的一段代码说明),基于声明的身份验证核心部分就是 var identity=new ClaimsIdentity(claims,"stringType"),声明这个后,当前用户上下文的IsAutheiticated将会被赋值为true

    OWIN的开源实现是Katana,实现了四个

    • Host: 托管我们应用程序的进程,或者宿主,可以是IIS,可以我们自己写的程序等。主要是用来启动,加载OWin组件,以及合理的关闭他们
    • Server: 这个Server就是用来暴露TCP端口,维护我们上面讲到的那个字典数据,然后通过OWin管理处理http请求
    • Middleware : 这个中间件就是用来在OWin管道中处理请求的组件,你可以把它想象成一个自定义的httpModule,它会被注册到OWin管道中一起处理http request
    • Application: 这个最好理解,就我们自己开发的那个应用程序或者说是网站

     以登陆为例,实现owin必须要有个声明入口starup(新建mvc后可以在appstart文件夹中看到)

    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            // 配置Middleware 組件选项,中间件是为了处理不同的业务例如下面的CookieAuthenticationMiddleware,可以参考他来自定义中间件,可以参考开源的owin--catana代码
          //这里是处理使用coocie登陆的中间件,是iappbuilder的扩展方法
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                CookieSecure = CookieSecureOption.Never,
            });
        }
    }

    这个是上门中间件扩展方法的实现

    public static IAppBuilder UseCookieAuthentication(this IAppBuilder app, 
        CookieAuthenticationOptions options)
    {
        if (app == null)
        {
            throw new ArgumentNullException("app");
        }
        app.Use(typeof(CookieAuthenticationMiddleware), app, options); //将组件注册进owin管道,--CookieAuthenticationMiddleware--组件是操作加密coocie的
    app.UseStageMarker(PipelineStage.Authenticate); // 然后结合例如IIS(owin的HOST)的某个阶段执行该组件,这里是认证阶段,还有七八种其他的例如post数据阶段等 return app; }
    劳神费力的,点歌赞打赏打赏
  • 相关阅读:
    Android 跳转到系统应用管理
    Android权限管理PermissionsDispatcher2.3.2使用+原生6.0权限使用
    Android权限管理PermissionsDispatcher2.3.2使用+原生6.0权限使用
    Android 6.0 Permission权限与安全机制
    Windows下Mongodb安装及配置
    mongo 3.0 备份和还原数据库 ,及too many positional arguments错误
    maven 将jar 下载到工程当前目录下
    MyBatis两张表字段名相同产生的问题
    svn报错can only be performed on a version resource [at this time].
    angular之interceptors拦截器
  • 原文地址:https://www.cnblogs.com/zfcflower/p/3671469.html
Copyright © 2011-2022 走看看