Migrated Identity.API to Identity Server 4 on dotnetcore2
This commit is contained in:
parent
7321d5e5fc
commit
aed97fdc99
@ -2,26 +2,23 @@
|
||||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
||||
|
||||
|
||||
using Identity.API.Models;
|
||||
using Identity.API.Models.AccountViewModels;
|
||||
using Identity.API.Services;
|
||||
using IdentityModel;
|
||||
using IdentityServer4.Quickstart.UI.Models;
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Services;
|
||||
using Microsoft.AspNetCore.Http.Authentication;
|
||||
using IdentityServer4.Stores;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Stores;
|
||||
using Identity.API.Services;
|
||||
using Identity.API.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Identity.API.Models.AccountViewModels;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
|
||||
namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
{
|
||||
@ -36,7 +33,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
private readonly ILoginService<ApplicationUser> _loginService;
|
||||
private readonly IIdentityServerInteractionService _interaction;
|
||||
private readonly IClientStore _clientStore;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger<AccountController> _logger;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public AccountController(
|
||||
@ -45,13 +42,13 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
ILoginService<ApplicationUser> loginService,
|
||||
IIdentityServerInteractionService interaction,
|
||||
IClientStore clientStore,
|
||||
ILoggerFactory loggerFactory,
|
||||
ILogger<AccountController> logger,
|
||||
UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_loginService = loginService;
|
||||
_interaction = interaction;
|
||||
_clientStore = clientStore;
|
||||
_logger = loggerFactory.CreateLogger<AccountController>();
|
||||
_logger = logger;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
@ -69,6 +66,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
}
|
||||
|
||||
var vm = await BuildLoginViewModelAsync(returnUrl, context);
|
||||
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
|
||||
return View(vm);
|
||||
@ -97,6 +95,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
};
|
||||
|
||||
await _loginService.SignIn(user);
|
||||
|
||||
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
|
||||
if (_interaction.IsValidReturnUrl(model.ReturnUrl))
|
||||
{
|
||||
@ -111,7 +110,9 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
|
||||
// something went wrong, show form with error
|
||||
var vm = await BuildLoginViewModelAsync(model);
|
||||
|
||||
ViewData["ReturnUrl"] = model.ReturnUrl;
|
||||
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
@ -180,6 +181,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
public async Task<IActionResult> Logout(LogoutViewModel model)
|
||||
{
|
||||
var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value;
|
||||
|
||||
if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider)
|
||||
{
|
||||
if (model.LogoutId == null)
|
||||
@ -191,10 +193,15 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
}
|
||||
|
||||
string url = "/Account/Logout?logoutId=" + model.LogoutId;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// hack: try/catch to handle social providers that throw
|
||||
await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url });
|
||||
await HttpContext.SignOutAsync(idp, new AuthenticationProperties
|
||||
{
|
||||
RedirectUri = url
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -203,7 +210,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
}
|
||||
|
||||
// delete authentication cookie
|
||||
await HttpContext.Authentication.SignOutAsync();
|
||||
await HttpContext.SignOutAsync();
|
||||
|
||||
// set this so UI rendering sees an anonymous user
|
||||
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());
|
||||
@ -217,7 +224,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers
|
||||
public async Task<IActionResult> DeviceLogOut(string redirectUrl)
|
||||
{
|
||||
// delete authentication cookie
|
||||
await HttpContext.Authentication.SignOutAsync();
|
||||
await HttpContext.SignOutAsync();
|
||||
|
||||
// set this so UI rendering sees an anonymous user
|
||||
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());
|
||||
|
@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Identity.API.Models;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Identity.API.Models;
|
||||
|
||||
namespace Identity.API.Data
|
||||
{
|
||||
|
@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp1.1</TargetFramework>
|
||||
<RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<UserSecretsId>aspnet-eShopOnContainers.Identity-90487118-103c-4ff0-b9da-e5e26f7ab0c5</UserSecretsId>
|
||||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
|
||||
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
|
||||
@ -16,32 +16,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink.Loader" Version="14.1.0" />
|
||||
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
|
||||
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.0.0-rc1" />
|
||||
<PackageReference Include="IdentityServer4.EntityFramework" Version="2.0.0-rc1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -51,10 +28,10 @@
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.4.337" />
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
|
||||
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild3-final" />
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
|
||||
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.5.357" />
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
|
||||
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Identity.API.Models
|
||||
|
@ -11,7 +11,6 @@ using IdentityServer4.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks;
|
||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
||||
@ -95,16 +94,21 @@ namespace eShopOnContainers.Identity
|
||||
services.AddIdentityServer(x => x.IssuerUri = "null")
|
||||
.AddSigningCredential(Certificate.Get())
|
||||
.AddAspNetIdentity<ApplicationUser>()
|
||||
.AddConfigurationStore(builder =>
|
||||
builder.UseSqlServer(connectionString, options =>
|
||||
options.MigrationsAssembly(migrationsAssembly)))
|
||||
.AddOperationalStore(builder =>
|
||||
builder.UseSqlServer(connectionString, options =>
|
||||
options.MigrationsAssembly(migrationsAssembly)))
|
||||
.AddConfigurationStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
|
||||
opts.MigrationsAssembly(migrationsAssembly));
|
||||
})
|
||||
.AddOperationalStore(options =>
|
||||
{
|
||||
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
|
||||
opts.MigrationsAssembly(migrationsAssembly));
|
||||
})
|
||||
.Services.AddTransient<IProfileService, ProfileService>();
|
||||
|
||||
var container = new ContainerBuilder();
|
||||
container.Populate(services);
|
||||
|
||||
return new AutofacServiceProvider(container.Build());
|
||||
}
|
||||
|
||||
@ -118,7 +122,6 @@ namespace eShopOnContainers.Identity
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseDatabaseErrorPage();
|
||||
app.UseBrowserLink();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -142,7 +145,7 @@ namespace eShopOnContainers.Identity
|
||||
await next();
|
||||
});
|
||||
|
||||
app.UseIdentity();
|
||||
app.UseAuthentication();
|
||||
|
||||
// Adds IdentityServer
|
||||
app.UseIdentityServer();
|
||||
|
Loading…
x
Reference in New Issue
Block a user