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.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<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _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<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IAuthenticationSchemeProvider schemes,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_schemes = schemes;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<ManageController>();
@ -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

View File

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

View File

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

View File

@ -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 @@
<p>
@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>
</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.
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
/// <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="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,
IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory<TUser> claimsFactory,
IOptions<IdentityOptions> optionsAccessor,
ILogger<SignInManager<TUser>> logger)
ILogger<SignInManager<TUser>> 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;
/// <summary>
/// Gets the <see cref="ILogger"/> used to log messages from the manager.
@ -316,7 +321,6 @@ namespace Microsoft.AspNetCore.Identity
return SignInResult.Failed;
}
/// <summary>
/// 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.</param>
/// <returns>The task object representing the asynchronous operation containing the <see name="SignInResult"/>
/// for the sign-in attempt.</returns>
public virtual async Task<SignInResult> TwoFactorSignInAsync(string provider, string code, bool isPersistent,
bool rememberClient)
public virtual async Task<SignInResult> 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);
}
/// <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>
/// Gets the external login information for the current login, as an asynchronous operation.
/// </summary>