Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Identity.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26123.0
VisualStudioVersion = 15.0.26228.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0F647068-6602-4E24-B1DC-8ED91481A50A}"
EndProject
Expand All @@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.Identity.A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Identity.Specification.Tests", "src\Microsoft.AspNetCore.Identity.Specification.Tests\Microsoft.AspNetCore.Identity.Specification.Tests.csproj", "{5608E828-DD54-4E2A-B73C-FC22268BE797}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.Cookies", "..\Security\src\Microsoft.AspNetCore.Authentication.Cookies\Microsoft.AspNetCore.Authentication.Cookies.csproj", "{D747780A-7C44-4714-AD6C-3B226A7EB2FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -130,6 +132,18 @@ Global
{5608E828-DD54-4E2A-B73C-FC22268BE797}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5608E828-DD54-4E2A-B73C-FC22268BE797}.Release|x86.ActiveCfg = Release|Any CPU
{5608E828-DD54-4E2A-B73C-FC22268BE797}.Release|x86.Build.0 = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|x86.ActiveCfg = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Debug|x86.Build.0 = Debug|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|Any CPU.Build.0 = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|x86.ActiveCfg = Release|Any CPU
{D747780A-7C44-4714-AD6C-3B226A7EB2FB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -144,5 +158,6 @@ Global
{4490894C-3572-4E63-86F1-EE5105CE8A06} = {0F647068-6602-4E24-B1DC-8ED91481A50A}
{6A74C6EA-B241-4D6B-BCE4-BF89EC1D2475} = {0F647068-6602-4E24-B1DC-8ED91481A50A}
{5608E828-DD54-4E2A-B73C-FC22268BE797} = {0F647068-6602-4E24-B1DC-8ED91481A50A}
{D747780A-7C44-4714-AD6C-3B226A7EB2FB} = {0F647068-6602-4E24-B1DC-8ED91481A50A}
EndGlobalSection
EndGlobal
7 changes: 6 additions & 1 deletion samples/IdentitySample.Mvc/Controllers/ManageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using IdentitySample.Models;
using IdentitySample.Models.ManageViewModels;
using IdentitySample.Services;
using Microsoft.AspNetCore.Authentication;

namespace IdentitySamples.Controllers
{
Expand All @@ -19,19 +20,22 @@ public class ManageController : Controller
{
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;

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>();
Expand Down Expand Up @@ -308,7 +312,8 @@ public async Task<IActionResult> ManageLogins(ManageMessageId? message = null)
return View("Error");
}
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;
return View(new ManageLoginsViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;

namespace IdentitySample.Models.ManageViewModels
Expand All @@ -11,6 +8,6 @@ public class ManageLoginsViewModel
{
public IList<UserLoginInfo> CurrentLogins { get; set; }

public IList<AuthenticationDescription> OtherLogins { get; set; }
public IList<AuthenticationScheme> OtherLogins { get; set; }
}
}
1 change: 0 additions & 1 deletion samples/IdentitySample.Mvc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

app.UseStaticFiles();

app.UseIdentity();
// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715

app.UseMvc(routes =>
Expand Down
8 changes: 5 additions & 3 deletions samples/IdentitySample.Mvc/Views/Account/Login.cshtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@using System.Collections.Generic
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication
@using Microsoft.AspNetCore.Authentication
@model LoginViewModel
@inject SignInManager<ApplicationUser> SignInManager
@inject IAuthenticationSchemeProvider SchemeProvider

@{
ViewData["Title"] = "Log in";
Expand Down Expand Up @@ -59,7 +60,8 @@
<h4>Use another service to log in.</h4>
<hr />
@{
var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList();
var schemes = await SchemeProvider.GetAllSchemesAsync();
var loginProviders = schemes.ToList();
if (loginProviders.Count == 0)
{
<div>
Expand All @@ -76,7 +78,7 @@
<p>
@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.AuthenticationScheme" title="Log in using your @provider.Name account">@provider.Name</button>
}
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<p>
@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>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -60,6 +61,7 @@ protected virtual bool ShouldSkipDbTests()
/// <param name="context"></param>
protected virtual void SetupIdentityServices(IServiceCollection services, object context = null)
{
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentity<TUser, TRole>(options =>
{
Expand Down
28 changes: 3 additions & 25 deletions src/Microsoft.AspNetCore.Identity/BuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Builder
{
/// <summary>
Expand All @@ -19,25 +15,7 @@ public static class BuilderExtensions
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
/// <returns>The <see cref="IApplicationBuilder"/> instance this method extends.</returns>
public static IApplicationBuilder UseIdentity(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}

var marker = app.ApplicationServices.GetService<IdentityMarkerService>();
if (marker == null)
{
throw new InvalidOperationException(Resources.MustCallAddIdentity);
}

var options = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>().Value;
app.UseCookieAuthentication(options.Cookies.ExternalCookie);
app.UseCookieAuthentication(options.Cookies.TwoFactorRememberMeCookie);
app.UseCookieAuthentication(options.Cookies.TwoFactorUserIdCookie);
app.UseCookieAuthentication(options.Cookies.ApplicationCookie);
return app;
}
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470", error: true)]
public static IApplicationBuilder UseIdentity(this IApplicationBuilder app) => app;
}
}
69 changes: 22 additions & 47 deletions src/Microsoft.AspNetCore.Identity/IdentityCookieOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Identity
{
Expand All @@ -14,95 +12,72 @@ namespace Microsoft.AspNetCore.Identity
public class IdentityCookieOptions
{
private static readonly string CookiePrefix = "Identity";
private static readonly string DefaultApplicationScheme = CookiePrefix + ".Application";
private static readonly string DefaultExternalScheme = CookiePrefix + ".External";
private static readonly string DefaultTwoFactorRememberMeScheme = CookiePrefix + ".TwoFactorRememberMe";
private static readonly string DefaultTwoFactorUserIdScheme = CookiePrefix + ".TwoFactorUserId";

/// <summary>
/// Constructs a new instance of <see cref="IdentityCookieOptions"/>.
/// The scheme used to identify application authentication cookies.
/// </summary>
public IdentityCookieOptions()
{
// Configure all of the cookie middlewares
ApplicationCookie = new CookieAuthenticationOptions
{
AuthenticationScheme = DefaultApplicationScheme,
AutomaticAuthenticate = true,
AutomaticChallenge = true,
LoginPath = new PathString("/Account/Login"),
Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
}
};
public static readonly string ApplicationScheme = CookiePrefix + ".Application";

ExternalCookie = new CookieAuthenticationOptions
{
AutomaticAuthenticate = false,
AuthenticationScheme = DefaultExternalScheme,
CookieName = DefaultExternalScheme,
ExpireTimeSpan = TimeSpan.FromMinutes(5)
};
/// <summary>
/// The scheme used to identify external authentication cookies.
/// </summary>
public static readonly string ExternalScheme = CookiePrefix + ".External";

TwoFactorRememberMeCookie = new CookieAuthenticationOptions
{
AutomaticAuthenticate = false,
AuthenticationScheme = DefaultTwoFactorRememberMeScheme,
CookieName = DefaultTwoFactorRememberMeScheme
};
/// <summary>
/// The scheme used to identify Two Factor authentication cookies for saving the Remember Me state.
/// </summary>
public static readonly string TwoFactorRememberMeScheme = CookiePrefix + ".TwoFactorRememberMe";

TwoFactorUserIdCookie = new CookieAuthenticationOptions
{
AutomaticAuthenticate = false,
AuthenticationScheme = DefaultTwoFactorUserIdScheme,
CookieName = DefaultTwoFactorUserIdScheme,
ExpireTimeSpan = TimeSpan.FromMinutes(5)
};
}
/// <summary>
/// The scheme used to identify Two Factor authentication cookies for round tripping user identities.
/// </summary>
public static readonly string TwoFactorUserIdScheme = CookiePrefix + ".TwoFactorUserId";

/// <summary>
/// The options for the application cookie.
/// </summary>
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470", error: true)]
public CookieAuthenticationOptions ApplicationCookie { get; set; }

/// <summary>
/// The options for the external cookie.
/// </summary>
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470", error: true)]
public CookieAuthenticationOptions ExternalCookie { get; set; }

/// <summary>
/// The options for the two factor remember me cookie.
/// </summary>
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470", error: true)]
public CookieAuthenticationOptions TwoFactorRememberMeCookie { get; set; }

/// <summary>
/// The options for the two factor user id cookie.
/// </summary>
[Obsolete("See https://go.microsoft.com/fwlink/?linkid=845470", error: true)]
public CookieAuthenticationOptions TwoFactorUserIdCookie { get; set; }

/// <summary>
/// Gets the scheme used to identify application authentication cookies.
/// </summary>
/// <value>The scheme used to identify application authentication cookies.</value>
public string ApplicationCookieAuthenticationScheme => ApplicationCookie?.AuthenticationScheme;
public string ApplicationCookieAuthenticationScheme { get; set; } = ApplicationScheme;

/// <summary>
/// Gets the scheme used to identify external authentication cookies.
/// </summary>
/// <value>The scheme used to identify external authentication cookies.</value>
public string ExternalCookieAuthenticationScheme => ExternalCookie?.AuthenticationScheme;
public string ExternalCookieAuthenticationScheme { get; set; } = ExternalScheme;

/// <summary>
/// Gets the scheme used to identify Two Factor authentication cookies for round tripping user identities.
/// </summary>
/// <value>The scheme used to identify user identity 2fa authentication cookies.</value>
public string TwoFactorUserIdCookieAuthenticationScheme => TwoFactorUserIdCookie?.AuthenticationScheme;
public string TwoFactorUserIdCookieAuthenticationScheme { get; set; } = TwoFactorUserIdScheme;

/// <summary>
/// Gets the scheme used to identify Two Factor authentication cookies for saving the Remember Me state.
/// </summary>
/// <value>The scheme used to identify remember me application authentication cookies.</value>
public string TwoFactorRememberMeCookieAuthenticationScheme => TwoFactorRememberMeCookie?.AuthenticationScheme;
public string TwoFactorRememberMeCookieAuthenticationScheme { get; set; } = TwoFactorRememberMeScheme;
}
}
10 changes: 0 additions & 10 deletions src/Microsoft.AspNetCore.Identity/IdentityMarkerService.cs

This file was deleted.

Loading