Fork me on GitHub

一步一步学习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的环境变量,可以扩展提交一些其他授权参数

 

posted @ 2017-09-13 15:29  龙码精神  阅读(868)  评论(0编辑  收藏  举报