Repository navigation
Expand file tree
/
Copy pathAuthenticationMiddleware.cs
More file actions
84 lines (73 loc) · 2.59 KB
/
Copy pathAuthenticationMiddleware.cs
File metadata and controls
84 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Authentication
{
public abstract class AuthenticationMiddleware<TOptions> where TOptions : AuthenticationOptions, new()
{
private readonly RequestDelegate _next;
protected AuthenticationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IOptions<TOptions> options,
[NotNull] ILoggerFactory loggerFactory,
[NotNull] IUrlEncoder encoder,
ConfigureOptions<TOptions> configureOptions)
{
if (configureOptions != null)
{
Options = options.GetNamedOptions(configureOptions.Name);
configureOptions.Configure(Options, configureOptions.Name);
}
else
{
Options = options.Options;
}
Logger = loggerFactory.CreateLogger(this.GetType().FullName);
UrlEncoder = encoder;
if (string.IsNullOrEmpty(Options.ClaimsIssuer))
{
// Default to something reasonable
Options.ClaimsIssuer = Options.AuthenticationScheme;
}
_next = next;
}
public string AuthenticationScheme { get; set; }
public TOptions Options { get; set; }
public ILogger Logger { get; set; }
public IUrlEncoder UrlEncoder { get; set; }
public async Task Invoke(HttpContext context)
{
var handler = CreateHandler();
await handler.Initialize(Options, context, Logger, UrlEncoder);
try
{
if (!await handler.InvokeAsync())
{
await _next(context);
}
}
catch (Exception)
{
try
{
handler.Faulted = true;
await handler.TeardownAsync();
}
catch (Exception)
{
// Don't mask the original exception
}
throw;
}
await handler.TeardownAsync();
}
protected abstract AuthenticationHandler<TOptions> CreateHandler();
}
}