Repository navigation
Expand file tree
/
Copy pathClaimsTransformationMiddleware.cs
More file actions
52 lines (47 loc) · 1.73 KB
/
Copy pathClaimsTransformationMiddleware.cs
File metadata and controls
52 lines (47 loc) · 1.73 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
// 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.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Authentication
{
public class ClaimsTransformationMiddleware
{
private readonly RequestDelegate _next;
public ClaimsTransformationMiddleware(
[NotNull] RequestDelegate next,
[NotNull] IOptions<ClaimsTransformationOptions> options,
ConfigureOptions<ClaimsTransformationOptions> configureOptions)
{
if (configureOptions != null)
{
Options = options.GetNamedOptions(configureOptions.Name);
configureOptions.Configure(Options, configureOptions.Name);
}
else
{
Options = options.Options;
}
_next = next;
}
public ClaimsTransformationOptions Options { get; set; }
public async Task Invoke(HttpContext context)
{
var handler = new ClaimsTransformationAuthenticationHandler(Options.Transformer);
handler.RegisterAuthenticationHandler(context.GetAuthentication());
try {
if (Options.Transformer != null)
{
context.User = await Options.Transformer.TransformAsync(context.User);
}
await _next(context);
}
finally
{
handler.UnregisterAuthenticationHandler(context.GetAuthentication());
}
}
}
}