-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Default Authentication using events (#16884)
- Loading branch information
1 parent
1fb3287
commit bd93e29
Showing
7 changed files
with
106 additions
and
71 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
79 changes: 79 additions & 0 deletions
79
src/OrchardCore.Modules/OrchardCore.Users/Services/ExternalLoginFormEvents.cs
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,79 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Mvc.Core.Utilities; | ||
using OrchardCore.Users.Controllers; | ||
using OrchardCore.Users.Events; | ||
using OrchardCore.Users.Models; | ||
|
||
namespace OrchardCore.Users.Services; | ||
|
||
public sealed class ExternalLoginFormEvents : ILoginFormEvent | ||
{ | ||
private readonly ExternalLoginOptions _externalLoginOptions; | ||
private readonly SignInManager<IUser> _signInManager; | ||
private readonly LinkGenerator _linkGenerator; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public ExternalLoginFormEvents( | ||
IOptions<ExternalLoginOptions> externalLoginOptions, | ||
SignInManager<IUser> signInManager, | ||
LinkGenerator linkGenerator, | ||
IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_externalLoginOptions = externalLoginOptions.Value; | ||
_signInManager = signInManager; | ||
_linkGenerator = linkGenerator; | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public Task IsLockedOutAsync(IUser user) | ||
=> Task.CompletedTask; | ||
|
||
public Task LoggedInAsync(IUser user) | ||
=> Task.CompletedTask; | ||
|
||
public async Task<IActionResult> LoggingInAsync() | ||
{ | ||
if (!_externalLoginOptions.UseExternalProviderIfOnlyOneDefined) | ||
{ | ||
return null; | ||
} | ||
|
||
var schemes = await _signInManager.GetExternalAuthenticationSchemesAsync(); | ||
|
||
if (schemes.Count() == 1) | ||
{ | ||
var provider = schemes.First().Name; | ||
|
||
var model = new RouteValueDictionary(); | ||
|
||
if (_httpContextAccessor.HttpContext.Request.Query.TryGetValue("returnUrl", out var returnUrlValue)) | ||
{ | ||
model.Add("returnUrl", returnUrlValue); | ||
} | ||
|
||
var redirectUrl = _linkGenerator.GetPathByAction(_httpContextAccessor.HttpContext, | ||
action: nameof(ExternalAuthenticationsController.ExternalLoginCallback), | ||
controller: typeof(ExternalAuthenticationsController).ControllerName(), | ||
values: model); | ||
|
||
return new ChallengeResult( | ||
authenticationScheme: provider, | ||
properties: _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public Task LoggingInAsync(string userName, Action<string, string> reportError) | ||
=> Task.CompletedTask; | ||
|
||
public Task LoggingInFailedAsync(string userName) | ||
=> Task.CompletedTask; | ||
|
||
public Task LoggingInFailedAsync(IUser user) | ||
=> Task.CompletedTask; | ||
} |
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