Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit aed97fd

Browse files
Unai Zorrilla CastroUnai Zorrilla Castro
authored andcommitted
Migrated Identity.API to Identity Server 4 on dotnetcore2
1 parent 7321d5e commit aed97fd

File tree

5 files changed

+48
-69
lines changed

5 files changed

+48
-69
lines changed

src/Services/Identity/Identity.API/Controllers/AccountController.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

44

5+
using Identity.API.Models;
6+
using Identity.API.Models.AccountViewModels;
7+
using Identity.API.Services;
58
using IdentityModel;
6-
using IdentityServer4.Quickstart.UI.Models;
9+
using IdentityServer4.Models;
710
using IdentityServer4.Services;
8-
using Microsoft.AspNetCore.Http.Authentication;
11+
using IdentityServer4.Stores;
12+
using Microsoft.AspNetCore.Authentication;
13+
using Microsoft.AspNetCore.Authorization;
14+
using Microsoft.AspNetCore.Identity;
915
using Microsoft.AspNetCore.Mvc;
16+
using Microsoft.Extensions.Logging;
1017
using System;
11-
using System.Collections.Generic;
1218
using System.Linq;
1319
using System.Security.Claims;
1420
using System.Text.Encodings.Web;
1521
using System.Threading.Tasks;
16-
using IdentityServer4.Models;
17-
using IdentityServer4.Stores;
18-
using Identity.API.Services;
19-
using Identity.API.Models;
20-
using Microsoft.Extensions.Logging;
21-
using Microsoft.AspNetCore.Authorization;
22-
using Identity.API.Models.AccountViewModels;
23-
using Microsoft.AspNetCore.Identity;
24-
using Microsoft.AspNetCore.Authentication;
2522

2623
namespace IdentityServer4.Quickstart.UI.Controllers
2724
{
@@ -36,7 +33,7 @@ public class AccountController : Controller
3633
private readonly ILoginService<ApplicationUser> _loginService;
3734
private readonly IIdentityServerInteractionService _interaction;
3835
private readonly IClientStore _clientStore;
39-
private readonly ILogger _logger;
36+
private readonly ILogger<AccountController> _logger;
4037
private readonly UserManager<ApplicationUser> _userManager;
4138

4239
public AccountController(
@@ -45,13 +42,13 @@ public AccountController(
4542
ILoginService<ApplicationUser> loginService,
4643
IIdentityServerInteractionService interaction,
4744
IClientStore clientStore,
48-
ILoggerFactory loggerFactory,
45+
ILogger<AccountController> logger,
4946
UserManager<ApplicationUser> userManager)
5047
{
5148
_loginService = loginService;
5249
_interaction = interaction;
5350
_clientStore = clientStore;
54-
_logger = loggerFactory.CreateLogger<AccountController>();
51+
_logger = logger;
5552
_userManager = userManager;
5653
}
5754

@@ -69,6 +66,7 @@ public async Task<IActionResult> Login(string returnUrl)
6966
}
7067

7168
var vm = await BuildLoginViewModelAsync(returnUrl, context);
69+
7270
ViewData["ReturnUrl"] = returnUrl;
7371

7472
return View(vm);
@@ -97,6 +95,7 @@ public async Task<IActionResult> Login(LoginViewModel model)
9795
};
9896

9997
await _loginService.SignIn(user);
98+
10099
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
101100
if (_interaction.IsValidReturnUrl(model.ReturnUrl))
102101
{
@@ -111,7 +110,9 @@ public async Task<IActionResult> Login(LoginViewModel model)
111110

112111
// something went wrong, show form with error
113112
var vm = await BuildLoginViewModelAsync(model);
113+
114114
ViewData["ReturnUrl"] = model.ReturnUrl;
115+
115116
return View(vm);
116117
}
117118

@@ -180,6 +181,7 @@ public async Task<IActionResult> Logout(string logoutId)
180181
public async Task<IActionResult> Logout(LogoutViewModel model)
181182
{
182183
var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value;
184+
183185
if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider)
184186
{
185187
if (model.LogoutId == null)
@@ -191,10 +193,15 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
191193
}
192194

193195
string url = "/Account/Logout?logoutId=" + model.LogoutId;
196+
194197
try
195198
{
199+
196200
// hack: try/catch to handle social providers that throw
197-
await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url });
201+
await HttpContext.SignOutAsync(idp, new AuthenticationProperties
202+
{
203+
RedirectUri = url
204+
});
198205
}
199206
catch (Exception ex)
200207
{
@@ -203,7 +210,7 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
203210
}
204211

205212
// delete authentication cookie
206-
await HttpContext.Authentication.SignOutAsync();
213+
await HttpContext.SignOutAsync();
207214

208215
// set this so UI rendering sees an anonymous user
209216
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());
@@ -217,7 +224,7 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
217224
public async Task<IActionResult> DeviceLogOut(string redirectUrl)
218225
{
219226
// delete authentication cookie
220-
await HttpContext.Authentication.SignOutAsync();
227+
await HttpContext.SignOutAsync();
221228

222229
// set this so UI rendering sees an anonymous user
223230
HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity());

src/Services/Identity/Identity.API/Data/ApplicationDbContext.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using Identity.API.Models;
52
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
63
using Microsoft.EntityFrameworkCore;
7-
using Identity.API.Models;
84

95
namespace Identity.API.Data
106
{

src/Services/Identity/Identity.API/Identity.API.csproj

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
5-
<RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
66
<UserSecretsId>aspnet-eShopOnContainers.Identity-90487118-103c-4ff0-b9da-e5e26f7ab0c5</UserSecretsId>
77
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
88
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
@@ -16,32 +16,9 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
19-
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
20-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
21-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.2" />
22-
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
23-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
24-
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.2" />
25-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
26-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
27-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
28-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
29-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2">
30-
<PrivateAssets>All</PrivateAssets>
31-
</PackageReference>
32-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1">
33-
<PrivateAssets>All</PrivateAssets>
34-
</PackageReference>
35-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
36-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
37-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.2" />
38-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
39-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
40-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
41-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
42-
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink.Loader" Version="14.1.0" />
43-
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
44-
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.1" />
19+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
20+
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="2.0.0-rc1" />
21+
<PackageReference Include="IdentityServer4.EntityFramework" Version="2.0.0-rc1" />
4522
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
4623
</ItemGroup>
4724

@@ -51,10 +28,10 @@
5128
</Target>
5229

5330
<ItemGroup>
54-
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.4.337" />
55-
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
56-
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild3-final" />
57-
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
31+
<DotNetCliToolReference Include="BundlerMinifier.Core" Version="2.5.357" />
32+
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
33+
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
34+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
5835
</ItemGroup>
5936

6037
<ItemGroup>

src/Services/Identity/Identity.API/Models/ApplicationUser.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
1+
using Microsoft.AspNetCore.Identity;
62
using System.ComponentModel.DataAnnotations;
73

84
namespace Identity.API.Models

src/Services/Identity/Identity.API/Startup.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.AspNetCore.Builder;
1212
using Microsoft.AspNetCore.Hosting;
1313
using Microsoft.AspNetCore.Identity;
14-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
1514
using Microsoft.EntityFrameworkCore;
1615
using Microsoft.eShopOnContainers.BuildingBlocks;
1716
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
@@ -95,16 +94,21 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
9594
services.AddIdentityServer(x => x.IssuerUri = "null")
9695
.AddSigningCredential(Certificate.Get())
9796
.AddAspNetIdentity<ApplicationUser>()
98-
.AddConfigurationStore(builder =>
99-
builder.UseSqlServer(connectionString, options =>
100-
options.MigrationsAssembly(migrationsAssembly)))
101-
.AddOperationalStore(builder =>
102-
builder.UseSqlServer(connectionString, options =>
103-
options.MigrationsAssembly(migrationsAssembly)))
97+
.AddConfigurationStore(options =>
98+
{
99+
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
100+
opts.MigrationsAssembly(migrationsAssembly));
101+
})
102+
.AddOperationalStore(options =>
103+
{
104+
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
105+
opts.MigrationsAssembly(migrationsAssembly));
106+
})
104107
.Services.AddTransient<IProfileService, ProfileService>();
105108

106109
var container = new ContainerBuilder();
107110
container.Populate(services);
111+
108112
return new AutofacServiceProvider(container.Build());
109113
}
110114

@@ -118,7 +122,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
118122
{
119123
app.UseDeveloperExceptionPage();
120124
app.UseDatabaseErrorPage();
121-
app.UseBrowserLink();
122125
}
123126
else
124127
{
@@ -142,7 +145,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
142145
await next();
143146
});
144147

145-
app.UseIdentity();
148+
app.UseAuthentication();
146149

147150
// Adds IdentityServer
148151
app.UseIdentityServer();

0 commit comments

Comments
 (0)