• 一步一步学习IdentityServer3 (7)


    在介绍自定义用户服务之前先对IdentityServerServiceFactory说明下 Idr3的服务工厂

    下面有很多idr3提供的接口服务,

    如:ViewService、UserService、ClientStore 等很多,可以做很多的事情

    其实它承载的不光是自身的接口服务,其实还提供了 服务注册 DI ,我们可以注册自己的接口服务实现

    自定义用户服务只需要去实现 IUserService接口就行了

      factory.UserService = new Registration<IUserService, IdrConfig.UserServices>();

    去看下IUserService接口中的方法

     //
            // 摘要:
            //     This method gets called when the user uses an external identity provider to authenticate.
            //     The user's identity from the external provider is passed via the `externalUser`
            //     parameter which contains the provider identifier, the provider's identifier for
            //     the user, and the claims from the provider for the external user.
            //
            // 参数:
            //   context:
            //     The context.
            Task AuthenticateExternalAsync(ExternalAuthenticationContext context);
            //
            // 摘要:
            //     This method gets called for local authentication (whenever the user uses the
            //     username and password dialog).
            //
            // 参数:
            //   context:
            //     The context.
            Task AuthenticateLocalAsync(LocalAuthenticationContext context);
            //
            // 摘要:
            //     This method is called whenever claims about the user are requested (e.g. during
            //     token creation or via the userinfo endpoint)
            //
            // 参数:
            //   context:
            //     The context.
            Task GetProfileDataAsync(ProfileDataRequestContext context);
            //
            // 摘要:
            //     This method gets called whenever identity server needs to determine if the user
            //     is valid or active (e.g. if the user's account has been deactivated since they
            //     logged in). (e.g. during token issuance or validation).
            //
            // 参数:
            //   context:
            //     The context.
            Task IsActiveAsync(IsActiveContext context);
            //
            // 摘要:
            //     This method is called prior to the user being issued a login cookie for IdentityServer.
            //
            // 参数:
            //   context:
            //     The context.
            Task PostAuthenticateAsync(PostAuthenticationContext context);
            //
            // 摘要:
            //     This method gets called before the login page is shown. This allows you to determine
            //     if the user should be authenticated by some out of band mechanism (e.g. client
            //     certificates or trusted headers).
            //
            // 参数:
            //   context:
            //     The context.
            Task PreAuthenticateAsync(PreAuthenticationContext context);
            //
            // 摘要:
            //     This method gets called when the user signs out.
            //
            // 参数:
            //   context:
            //     The context.
            Task SignOutAsync(SignOutContext context);
    IUserService

    我们在自己定义的 UserServices中去实现这些接口方法

     OwinContext ctx;
    //修改  这里依赖我们注册的接口服务
    ILYMUser _userServices;

    public UserServices(OwinEnvironmentService owinEnv,ILYMUser userServices) {

    ctx = new OwinContext(owinEnv.Environment); 

    _userServices=userServices;

    }

    到了这一步,其实只是去实现它的接口,在接口方法实现中我们要用自己的接口服务怎么办呢?

    其实只需要在factory上注册自己的 服务接口就行了,然后在创建UserServices 构造函数是依赖我们之前注册的自定义服务接口就ok了

       factory.Register(new Registration<ILYMUser, LYMUser>());
                    factory.UserService = new Registration<IUserService, IdrConfig.UserServices>();

    这里的ILYMUser、LYMUser自定的接口服务,定义好登录相关方法,在UserServices中本地身份验证的中实现先关业务逻辑就ok

    AuthenticateLocalAsync:当用户使用该方法时,该方法将调用本地身份验证 用户名和密码对话框

    Tips:这里Owin中间件上下文对象需要创建idr3的环境变量,可以扩展提交一些其他授权参数

  • 相关阅读:
    Unity3D启动时卡在Loading界面
    unity触发器碰撞调用OnTriggerStay()检查按键多次执行的问题
    Unity使用Destroy删除物体的问题
    Unity脚本设置场景环境光,实现白天晚上切换
    unity隐藏显示物体
    unity使用Vuplex的WebView嵌入网页,打包后报错进不去
    【Python】输入两个字符串,在第一字符串中删除第二个字符串中所包含的所有字符
    用until编写一段shell程序,计算1~10的平方
    用until编写一段shell程序,计算1~10的平方和
    PrismJS,一款漂亮的代码高亮工具
  • 原文地址:https://www.cnblogs.com/liyouming/p/7515173.html
Copyright © 2020-2023  润新知