Last active
July 3, 2017 13:54
-
-
Save aaronhoffman/e33715dc4e28a025299762aedcaeec3f to your computer and use it in GitHub Desktop.
asp.net core AuthenticationMiddleware Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyAuthenticationMiddleware : AuthenticationMiddleware<MyAuthenticationOptions> | |
| { | |
| public MyAuthenticationMiddleware( | |
| IMyService myService, // inject service here?? (via Startup.ConfigureServices(), services.AddTransient() ) | |
| RequestDelegate next, | |
| IOptions<MyAuthenticationOptions> options, | |
| ILoggerFactory loggerFactory, | |
| UrlEncoder encoder) | |
| : base(next, options, loggerFactory, encoder) | |
| { | |
| _myService = myService; | |
| } | |
| private IMyService _myService { get; } | |
| protected override AuthenticationHandler<MyAuthenticationOptions> CreateHandler(/* inject service here somehow? */) | |
| { | |
| // pass singleton instance to scope instance? | |
| // use DI to resolve instead of `new` instance here? | |
| return new MyAuthenticationHandler(_myService); | |
| } | |
| } | |
| public class MyAuthenticationOptions : AuthenticationOptions | |
| { | |
| public MyAuthenticationOptions(/* inject here not allowed, empty cstr required. */) | |
| { | |
| // injection not possible | |
| } | |
| } | |
| public class PCSAuthenticationHandler : AuthenticationHandler<MyAuthenticationOptions> | |
| { | |
| public MyAuthenticationHandler( | |
| IMyService myService) // inject here somehow?? | |
| { | |
| _myService = myService; | |
| } | |
| private IMyService _myService { get; } | |
| protected override async Task<AuthenticateResult> HandleAuthenticateAsync(/* or inject here maybe? */) | |
| { | |
| await Task.Yield(); | |
| // need _myService.MyAwesomeFunction() here... | |
| return AuthenticateResult.Fail("Dependency Injection"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment