/
SignInResult.cs
95 lines (82 loc) · 4.13 KB
/
SignInResult.cs
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
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// An <see cref="ActionResult"/> that on execution invokes <see cref="M:HttpContext.SignInAsync"/>.
/// </summary>
public partial class SignInResult : ActionResult
{
/// <summary>
/// Initializes a new instance of <see cref="SignInResult"/> with the
/// default authentication scheme.
/// </summary>
/// <param name="principal">The claims principal containing the user claims.</param>
public SignInResult(ClaimsPrincipal principal)
: this(authenticationScheme: null, principal, properties: null)
{
}
/// <summary>
/// Initializes a new instance of <see cref="SignInResult"/> with the
/// specified authentication scheme.
/// </summary>
/// <param name="authenticationScheme">The authentication scheme to use when signing in the user.</param>
/// <param name="principal">The claims principal containing the user claims.</param>
public SignInResult(string? authenticationScheme, ClaimsPrincipal principal)
: this(authenticationScheme, principal, properties: null)
{
}
/// <summary>
/// Initializes a new instance of <see cref="SignInResult"/> with the
/// default authentication scheme and <paramref name="properties"/>.
/// </summary>
/// <param name="principal">The claims principal containing the user claims.</param>
/// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the sign-in operation.</param>
public SignInResult(ClaimsPrincipal principal, AuthenticationProperties? properties)
: this(authenticationScheme: null, principal, properties)
{
}
/// <summary>
/// Initializes a new instance of <see cref="SignInResult"/> with the
/// specified authentication scheme and <paramref name="properties"/>.
/// </summary>
/// <param name="authenticationScheme">The authentication schemes to use when signing in the user.</param>
/// <param name="principal">The claims principal containing the user claims.</param>
/// <param name="properties"><see cref="AuthenticationProperties"/> used to perform the sign-in operation.</param>
public SignInResult(string? authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties? properties)
{
Principal = principal ?? throw new ArgumentNullException(nameof(principal));
AuthenticationScheme = authenticationScheme;
Properties = properties;
}
/// <summary>
/// Gets or sets the authentication scheme that is used to perform the sign-in operation.
/// </summary>
public string? AuthenticationScheme { get; set; }
/// <summary>
/// Gets or sets the <see cref="ClaimsPrincipal"/> containing the user claims.
/// </summary>
public ClaimsPrincipal Principal { get; set; }
/// <summary>
/// Gets or sets the <see cref="AuthenticationProperties"/> used to perform the sign-in operation.
/// </summary>
public AuthenticationProperties? Properties { get; set; }
/// <inheritdoc />
public override Task ExecuteResultAsync(ActionContext context)
{
ArgumentNullException.ThrowIfNull(context);
var httpContext = context.HttpContext;
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<SignInResult>();
Log.SignInResultExecuting(logger, AuthenticationScheme, Principal);
return httpContext.SignInAsync(AuthenticationScheme, Principal, Properties);
}
private static partial class Log
{
[LoggerMessage(1, LogLevel.Information, $"Executing {nameof(SignInResult)} with authentication scheme ({{Scheme}}) and the following principal: {{Principal}}.", EventName = "SignInResultExecuting")]
public static partial void SignInResultExecuting(ILogger logger, string? scheme, ClaimsPrincipal principal);
}
}