Add GetExternalAuthenticationSchemes back

This commit is contained in:
Hao Kung 2017-04-20 15:46:20 -07:00
parent c142085695
commit bc757faeba
5 changed files with 25 additions and 21 deletions

View File

@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Security.Claims; using IdentitySample.Models;
using Microsoft.AspNetCore.Authentication; using IdentitySample.Models.ManageViewModels;
using IdentitySample.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using IdentitySample.Models;
using IdentitySample.Models.ManageViewModels;
using IdentitySample.Services;
namespace IdentitySamples.Controllers namespace IdentitySamples.Controllers
{ {
@ -20,7 +16,6 @@ namespace IdentitySamples.Controllers
{ {
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IAuthenticationSchemeProvider _schemes;
private readonly IEmailSender _emailSender; private readonly IEmailSender _emailSender;
private readonly ISmsSender _smsSender; private readonly ISmsSender _smsSender;
private readonly ILogger _logger; private readonly ILogger _logger;
@ -28,14 +23,12 @@ namespace IdentitySamples.Controllers
public ManageController( public ManageController(
UserManager<ApplicationUser> userManager, UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, SignInManager<ApplicationUser> signInManager,
IAuthenticationSchemeProvider schemes,
IEmailSender emailSender, IEmailSender emailSender,
ISmsSender smsSender, ISmsSender smsSender,
ILoggerFactory loggerFactory) ILoggerFactory loggerFactory)
{ {
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
_schemes = schemes;
_emailSender = emailSender; _emailSender = emailSender;
_smsSender = smsSender; _smsSender = smsSender;
_logger = loggerFactory.CreateLogger<ManageController>(); _logger = loggerFactory.CreateLogger<ManageController>();
@ -312,7 +305,7 @@ namespace IdentitySamples.Controllers
return View("Error"); return View("Error");
} }
var userLogins = await _userManager.GetLoginsAsync(user); 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(); var otherLogins = schemes.Where(auth => userLogins.All(ul => auth.Name != ul.LoginProvider)).ToList();
ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1; ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1;
return View(new ManageLoginsViewModel return View(new ManageLoginsViewModel

View File

@ -71,5 +71,4 @@ namespace IdentitySample
}); });
} }
} }
} }

View File

@ -3,7 +3,6 @@
@using Microsoft.AspNetCore.Authentication @using Microsoft.AspNetCore.Authentication
@model LoginViewModel @model LoginViewModel
@inject SignInManager<ApplicationUser> SignInManager @inject SignInManager<ApplicationUser> SignInManager
@inject IAuthenticationSchemeProvider SchemeProvider
@{ @{
ViewData["Title"] = "Log in"; ViewData["Title"] = "Log in";
@ -60,7 +59,7 @@
<h4>Use another service to log in.</h4> <h4>Use another service to log in.</h4>
<hr /> <hr />
@{ @{
var schemes = await SchemeProvider.GetAllSchemesAsync(); var schemes = await SignInManager.GetExternalAuthenticationSchemesAsync();
var loginProviders = schemes.ToList(); var loginProviders = schemes.ToList();
if (loginProviders.Count == 0) if (loginProviders.Count == 0)
{ {

View File

@ -1,5 +1,5 @@
@model ManageLoginsViewModel @model ManageLoginsViewModel
@using Microsoft.AspNetCore.Http.Authentication @using Microsoft.AspNetCore.Authentication
@{ @{
ViewData["Title"] = "Manage your external logins"; ViewData["Title"] = "Manage your external logins";
} }
@ -46,7 +46,7 @@
<p> <p>
@foreach (var provider in Model.OtherLogins) @foreach (var provider in Model.OtherLogins)
{ {
<button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.Name account">@provider.Name</button> <button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.Name account">@provider.DisplayName</button>
} }
</p> </p>
</div> </div>

View File

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