SignalR => Auth 2.0

This commit is contained in:
Hao Kung 2017-04-19 18:51:24 -07:00
parent 8c8f6c708b
commit 27e90edc1c
5 changed files with 14 additions and 9 deletions

View File

@ -20,6 +20,7 @@ namespace ChatSample.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;
@ -27,12 +28,14 @@ namespace ChatSample.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>();
@ -290,7 +293,8 @@ namespace ChatSample.Controllers
return View("Error"); return View("Error");
} }
var userLogins = await _userManager.GetLoginsAsync(user); var userLogins = await _userManager.GetLoginsAsync(user);
var otherLogins = _signInManager.GetExternalAuthenticationSchemes().Where(auth => userLogins.All(ul => auth.AuthenticationScheme != ul.LoginProvider)).ToList(); var schemes = await _schemes.GetAllSchemesAsync();
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

@ -5,7 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
namespace ChatSample.Models.ManageViewModels namespace ChatSample.Models.ManageViewModels
@ -14,6 +14,6 @@ namespace ChatSample.Models.ManageViewModels
{ {
public IList<UserLoginInfo> CurrentLogins { get; set; } public IList<UserLoginInfo> CurrentLogins { get; set; }
public IList<AuthenticationDescription> OtherLogins { get; set; } public IList<AuthenticationScheme> OtherLogins { get; set; }
} }
} }

View File

@ -76,7 +76,7 @@ namespace ChatSample
app.UseStaticFiles(); app.UseStaticFiles();
app.UseIdentity(); app.UseAuthentication();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
//app.UseCookieAuthentication(); //app.UseCookieAuthentication();

View File

@ -1,6 +1,6 @@
@using System.Collections.Generic @using System.Collections.Generic
@using Microsoft.AspNetCore.Http @using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication @using Microsoft.AspNetCore.Authentication
@model LoginViewModel @model LoginViewModel
@inject SignInManager<ApplicationUser> SignInManager @inject SignInManager<ApplicationUser> SignInManager
@ -59,7 +59,8 @@
<h4>Use another service to log in.</h4> <h4>Use another service to log in.</h4>
<hr /> <hr />
@{ @{
var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList(); var schemes = await SchemeProvider.GetAllSchemesAsync();
var loginProviders = schemes.ToList();
if (loginProviders.Count == 0) if (loginProviders.Count == 0)
{ {
<div> <div>
@ -76,7 +77,7 @@
<p> <p>
@foreach (var provider in loginProviders) @foreach (var provider in loginProviders)
{ {
<button type="submit" class="btn btn-default" name="provider" value="@provider.AuthenticationScheme" title="Log in using your @provider.DisplayName account">@provider.AuthenticationScheme</button> <button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.Name account">@provider.Name</button>
} }
</p> </p>
</div> </div>

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.AuthenticationScheme" title="Log in using your @provider.DisplayName account">@provider.AuthenticationScheme</button> <button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.Name account">@provider.Name</button>
} }
</p> </p>
</div> </div>