新文章 网摘 文章 随笔 日记

IdentityServer3 Custom Services

 

IdentityServer3 provides many extensibility points for storage of data, validation logic and general functionality that are needed to support IdentityServer’s operation as a token service. These various extensibility points are collectively referred to as “services”.

See here for the full list of the services.

Mandatory services

Three of the services are mandatory and must be configured by the implementer, they are:

  • the user service (IUserService)

  • the client store (IClientStore)

  • the scope store (IScopeStore)

We provide a simple in-memory version of these three services as well as support for data stores via the related repos or community projects. See here for more details.

Registering custom Services

You can replace every service and register additional custom ones. This is encapsulated by the Registration class. A Registration represents a way for IdentityServer to obtain an instance of your service.

Depending upon the design of your service you might want to have a new instance on every request, use a singleton, or you might require special instantiation logic each time an instance is needed. To accommodate these different possibilities, the Registration class provides many different constructors to register your service:

  • new Registration<T>(Type yourImplementation)
    • Registers yourImplementation as the class that implements the T interface.
  • new Registration<T, Impl>()
    • Registers Impl as the class that implements the T interface. This API is simply a convenience for the previous that accepts a Type parameter.
  • new Registration<T>(T singleton)
    • Registers the singleton instance passed as a singleton implementation of the T interface.
  • new Registration<T>(Func<IDependencyResolver, T> factory)
    • Registers a callback function that will be invoked to return an implementation of the T interface.
var factory = new IdentityServerServiceFactory();
factory.UserService = new Registration<IUserService, MyCustomUserService>();

See the page on Dependency Injection (DI) page for more details.

Service cleanup

In all cases except for the singleton, if your service implements IDisposable, then Dispose will be called at the end of the HTTP request.

 示例:

https://github.com/IdentityServer/IdentityServer3.Samples

Dapper的实现:

https://github.com/ranga543/IdentityServer3.Dapper

来源:

https://identityserver.github.io/Documentation/docsv2/advanced/customServices.html

posted @ 2020-05-29 13:24  岭南春  阅读(220)  评论(0编辑  收藏  举报