This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce SignInResult/SignOutResult and ControllerBase.SignIn/SignOut
- Loading branch information
1 parent
49e0a95
commit f9d24a8
Showing
11 changed files
with
491 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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.Authentication; | ||
using Microsoft.AspNetCore.Mvc.Core; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Mvc | ||
{ | ||
/// <summary> | ||
/// An <see cref="ActionResult"/> that on execution invokes <see cref="M:AuthenticationManager.SignInAsync"/>. | ||
/// </summary> | ||
public class SignInResult : ActionResult | ||
{ | ||
/// <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 | ||
/// 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) | ||
{ | ||
if (authenticationScheme == null) | ||
{ | ||
throw new ArgumentNullException(nameof(authenticationScheme)); | ||
} | ||
|
||
if (principal == null) | ||
{ | ||
throw new ArgumentNullException(nameof(principal)); | ||
} | ||
|
||
AuthenticationScheme = authenticationScheme; | ||
Principal = principal; | ||
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 async Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (AuthenticationScheme == null) | ||
{ | ||
throw new InvalidOperationException( | ||
Resources.FormatPropertyOfTypeCannotBeNull( | ||
/* property: */ nameof(AuthenticationScheme), | ||
/* type: */ nameof(SignInResult))); | ||
} | ||
|
||
var loggerFactory = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>(); | ||
var logger = loggerFactory.CreateLogger<SignInResult>(); | ||
|
||
logger.SignInResultExecuting(AuthenticationScheme, Principal); | ||
|
||
var authentication = context.HttpContext.Authentication; | ||
await authentication.SignInAsync(AuthenticationScheme, Principal, Properties); | ||
} | ||
} | ||
} |
Oops, something went wrong.