Repository navigation
Expand file tree
/
Copy pathAuthenticationMiddleware.cs
More file actions
67 lines (60 loc) · 2.04 KB
/
Copy pathAuthenticationMiddleware.cs
File metadata and controls
67 lines (60 loc) · 2.04 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
// Copyright (c) Microsoft Open Technologies, Inc. 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.OptionsModel;
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,
ConfigureOptions<TOptions> configureOptions)
{
if (configureOptions != null)
{
Options = options.GetNamedOptions(configureOptions.Name);
configureOptions.Configure(Options, configureOptions.Name);
}
else
{
Options = options.Options;
}
_next = next;
}
public string AuthenticationScheme { get; set; }
public TOptions Options { get; set; }
public async Task Invoke(HttpContext context)
{
AuthenticationHandler<TOptions> handler = CreateHandler();
await handler.Initialize(Options, context);
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();
}
}