diff --git a/samples/IdentitySample.Mvc/Controllers/ManageController.cs b/samples/IdentitySample.Mvc/Controllers/ManageController.cs index 4c4c917e68..628345fbe5 100644 --- a/samples/IdentitySample.Mvc/Controllers/ManageController.cs +++ b/samples/IdentitySample.Mvc/Controllers/ManageController.cs @@ -1,16 +1,12 @@ -using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using System.Security.Claims; -using Microsoft.AspNetCore.Authentication; +using IdentitySample.Models; +using IdentitySample.Models.ManageViewModels; +using IdentitySample.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using IdentitySample.Models; -using IdentitySample.Models.ManageViewModels; -using IdentitySample.Services; namespace IdentitySamples.Controllers { @@ -20,7 +16,6 @@ namespace IdentitySamples.Controllers { private readonly UserManager _userManager; private readonly SignInManager _signInManager; - private readonly IAuthenticationSchemeProvider _schemes; private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; private readonly ILogger _logger; @@ -28,14 +23,12 @@ namespace IdentitySamples.Controllers public ManageController( UserManager userManager, SignInManager signInManager, - IAuthenticationSchemeProvider schemes, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory) { _userManager = userManager; _signInManager = signInManager; - _schemes = schemes; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger(); @@ -312,7 +305,7 @@ namespace IdentitySamples.Controllers return View("Error"); } var userLogins = await _userManager.GetLoginsAsync(user); - var schemes = await _schemes.GetAllSchemesAsync(); + var schemes = await _signInManager.GetExternalAuthenticationSchemesAsync(); var otherLogins = schemes.Where(auth => userLogins.All(ul => auth.Name != ul.LoginProvider)).ToList(); ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1; return View(new ManageLoginsViewModel diff --git a/samples/IdentitySample.Mvc/Startup.cs b/samples/IdentitySample.Mvc/Startup.cs index 1b337c0185..9b63d0ecbe 100644 --- a/samples/IdentitySample.Mvc/Startup.cs +++ b/samples/IdentitySample.Mvc/Startup.cs @@ -71,5 +71,4 @@ namespace IdentitySample }); } } -} - +} \ No newline at end of file diff --git a/samples/IdentitySample.Mvc/Views/Account/Login.cshtml b/samples/IdentitySample.Mvc/Views/Account/Login.cshtml index 439d0b335f..00e0e9ca33 100644 --- a/samples/IdentitySample.Mvc/Views/Account/Login.cshtml +++ b/samples/IdentitySample.Mvc/Views/Account/Login.cshtml @@ -3,7 +3,6 @@ @using Microsoft.AspNetCore.Authentication @model LoginViewModel @inject SignInManager SignInManager -@inject IAuthenticationSchemeProvider SchemeProvider @{ ViewData["Title"] = "Log in"; @@ -60,7 +59,7 @@

Use another service to log in.


@{ - var schemes = await SchemeProvider.GetAllSchemesAsync(); + var schemes = await SignInManager.GetExternalAuthenticationSchemesAsync(); var loginProviders = schemes.ToList(); if (loginProviders.Count == 0) { diff --git a/samples/IdentitySample.Mvc/Views/Manage/ManageLogins.cshtml b/samples/IdentitySample.Mvc/Views/Manage/ManageLogins.cshtml index 095e186530..5fe5ed475e 100644 --- a/samples/IdentitySample.Mvc/Views/Manage/ManageLogins.cshtml +++ b/samples/IdentitySample.Mvc/Views/Manage/ManageLogins.cshtml @@ -1,5 +1,5 @@ @model ManageLoginsViewModel -@using Microsoft.AspNetCore.Http.Authentication +@using Microsoft.AspNetCore.Authentication @{ ViewData["Title"] = "Manage your external logins"; } @@ -46,7 +46,7 @@

@foreach (var provider in Model.OtherLogins) { - + }

diff --git a/src/Microsoft.AspNetCore.Identity/SignInManager.cs b/src/Microsoft.AspNetCore.Identity/SignInManager.cs index 6eae09e110..f40afcd499 100644 --- a/src/Microsoft.AspNetCore.Identity/SignInManager.cs +++ b/src/Microsoft.AspNetCore.Identity/SignInManager.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; @@ -31,11 +32,13 @@ namespace Microsoft.AspNetCore.Identity /// The factory to use to create claims principals for a user. /// The accessor used to access the . /// The logger used to log messages, warnings and errors. + /// The logger used to log messages, warnings and errors. public SignInManager(UserManager userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory claimsFactory, IOptions optionsAccessor, - ILogger> logger) + ILogger> logger, + IAuthenticationSchemeProvider schemes) { if (userManager == null) { @@ -55,10 +58,12 @@ namespace Microsoft.AspNetCore.Identity ClaimsFactory = claimsFactory; Options = optionsAccessor?.Value ?? new IdentityOptions(); Logger = logger; + _schemes = schemes; } private readonly IHttpContextAccessor _contextAccessor; private HttpContext _context; + private IAuthenticationSchemeProvider _schemes; /// /// Gets the used to log messages from the manager. @@ -316,7 +321,6 @@ namespace Microsoft.AspNetCore.Identity return SignInResult.Failed; } - /// /// Returns a flag indicating if the current client browser has been remembered by two factor authentication /// for the user attempting to login, as an asynchronous operation. @@ -454,8 +458,7 @@ namespace Microsoft.AspNetCore.Identity /// two factor authentication prompts. /// The task object representing the asynchronous operation containing the /// for the sign-in attempt. - public virtual async Task TwoFactorSignInAsync(string provider, string code, bool isPersistent, - bool rememberClient) + public virtual async Task TwoFactorSignInAsync(string provider, string code, bool isPersistent, bool rememberClient) { var twoFactorInfo = await RetrieveTwoFactorInfoAsync(); if (twoFactorInfo == null || twoFactorInfo.UserId == null) @@ -535,6 +538,16 @@ namespace Microsoft.AspNetCore.Identity return await SignInOrTwoFactorAsync(user, isPersistent, loginProvider, bypassTwoFactor); } + /// + /// Gets a collection of s for the known external login providers. + /// + /// A collection of s for the known external login providers. + public virtual async Task> GetExternalAuthenticationSchemesAsync() + { + var schemes = await _schemes.GetAllSchemesAsync(); + return schemes.Where(s => !string.IsNullOrEmpty(s.DisplayName)); + } + /// /// Gets the external login information for the current login, as an asynchronous operation. ///