Repository navigation
Expand file tree
/
Copy pathVirtualAuthenticationHandler.cs
More file actions
71 lines (58 loc) · 2.71 KB
/
Copy pathVirtualAuthenticationHandler.cs
File metadata and controls
71 lines (58 loc) · 2.71 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
// 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.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Authentication
{
/// <summary>
/// Forwards calls to another authentication scheme.
/// </summary>
public class VirtualAuthenticationHandler : IAuthenticationHandler, IAuthenticationSignInHandler
{
protected IOptionsMonitor<VirtualSchemeOptions> OptionsMonitor { get; }
public AuthenticationScheme Scheme { get; private set; }
public VirtualSchemeOptions Options { get; private set; }
protected HttpContext Context { get; private set; }
public VirtualAuthenticationHandler(IOptionsMonitor<VirtualSchemeOptions> options)
{
OptionsMonitor = options;
}
/// <summary>
/// Initialize the handler, resolve the options and validate them.
/// </summary>
/// <param name="scheme"></param>
/// <param name="context"></param>
/// <returns>A Task.</returns>
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
Scheme = scheme;
Context = context;
Options = OptionsMonitor.Get(Scheme.Name) ?? new VirtualSchemeOptions();
Options.Validate();
return Task.CompletedTask;
}
protected virtual string ResolveTarget(string scheme)
=> scheme ?? Options.DefaultSelector?.Invoke(Context) ?? Options.Default;
public virtual Task<AuthenticateResult> AuthenticateAsync()
=> Context.AuthenticateAsync(ResolveTarget(Options.Authenticate));
public virtual Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
=> Context.SignInAsync(ResolveTarget(Options.SignIn), user, properties);
public virtual Task SignOutAsync(AuthenticationProperties properties)
=> Context.SignOutAsync(ResolveTarget(Options.SignOut), properties);
public virtual Task ChallengeAsync(AuthenticationProperties properties)
=> Context.ChallengeAsync(ResolveTarget(Options.Challenge), properties);
public virtual Task ForbidAsync(AuthenticationProperties properties)
=> Context.ForbidAsync(ResolveTarget(Options.Forbid), properties);
}
}