Skip to content

Commit ddba962

Browse files
authored
Upgrade to ASP.NET Core 2 (#3)
1 parent 30cb433 commit ddba962

File tree

9 files changed

+29
-70
lines changed

9 files changed

+29
-70
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,4 @@ paket-files/
250250
# JetBrains Rider
251251
.idea/
252252
*.sln.iml
253+
/Sqlite/Sqlite/Database.db

Sqlite/Sqlite/Controllers/AccountController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.AspNetCore.Mvc;
99
using Microsoft.AspNetCore.Mvc.Rendering;
1010
using Microsoft.Extensions.Logging;
11-
using Microsoft.Extensions.Options;
1211
using Sqlite.Models;
1312
using Sqlite.Models.AccountViewModels;
1413
using Sqlite.Services;
@@ -23,19 +22,16 @@ public class AccountController : Controller
2322
private readonly IEmailSender _emailSender;
2423
private readonly ISmsSender _smsSender;
2524
private readonly ILogger _logger;
26-
private readonly string _externalCookieScheme;
2725

2826
public AccountController(
2927
UserManager<ApplicationUser> userManager,
3028
SignInManager<ApplicationUser> signInManager,
31-
IOptions<IdentityCookieOptions> identityCookieOptions,
3229
IEmailSender emailSender,
3330
ISmsSender smsSender,
3431
ILoggerFactory loggerFactory)
3532
{
3633
_userManager = userManager;
3734
_signInManager = signInManager;
38-
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
3935
_emailSender = emailSender;
4036
_smsSender = smsSender;
4137
_logger = loggerFactory.CreateLogger<AccountController>();
@@ -48,7 +44,7 @@ public AccountController(
4844
public async Task<IActionResult> Login(string returnUrl = null)
4945
{
5046
// Clear the existing external cookie to ensure a clean login process
51-
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
47+
await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme);
5248

5349
ViewData["ReturnUrl"] = returnUrl;
5450
return View();

Sqlite/Sqlite/Controllers/ManageController.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@ public class ManageController : Controller
1818
{
1919
private readonly UserManager<ApplicationUser> _userManager;
2020
private readonly SignInManager<ApplicationUser> _signInManager;
21-
private readonly string _externalCookieScheme;
2221
private readonly IEmailSender _emailSender;
2322
private readonly ISmsSender _smsSender;
2423
private readonly ILogger _logger;
2524

2625
public ManageController(
2726
UserManager<ApplicationUser> userManager,
2827
SignInManager<ApplicationUser> signInManager,
29-
IOptions<IdentityCookieOptions> identityCookieOptions,
3028
IEmailSender emailSender,
3129
ISmsSender smsSender,
3230
ILoggerFactory loggerFactory)
3331
{
3432
_userManager = userManager;
3533
_signInManager = signInManager;
36-
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
3734
_emailSender = emailSender;
3835
_smsSender = smsSender;
3936
_logger = loggerFactory.CreateLogger<ManageController>();
@@ -291,7 +288,7 @@ public async Task<IActionResult> ManageLogins(ManageMessageId? message = null)
291288
return View("Error");
292289
}
293290
var userLogins = await _userManager.GetLoginsAsync(user);
294-
var otherLogins = _signInManager.GetExternalAuthenticationSchemes().Where(auth => userLogins.All(ul => auth.AuthenticationScheme != ul.LoginProvider)).ToList();
291+
var otherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).Where(auth => userLogins.All(ul => auth.Name != ul.LoginProvider)).ToList();
295292
ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1;
296293
return View(new ManageLoginsViewModel
297294
{
@@ -307,7 +304,7 @@ public async Task<IActionResult> ManageLogins(ManageMessageId? message = null)
307304
public async Task<IActionResult> LinkLogin(string provider)
308305
{
309306
// Clear the existing external cookie to ensure a clean login process
310-
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
307+
await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme);
311308

312309
// Request a redirect to the external login provider to link a login for the current user
313310
var redirectUrl = Url.Action(nameof(LinkLoginCallback), "Manage");
@@ -336,7 +333,7 @@ public async Task<ActionResult> LinkLoginCallback()
336333
{
337334
message = ManageMessageId.AddLoginSuccess;
338335
// Clear the existing external cookie to ensure a clean login process
339-
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);
336+
await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme);
340337
}
341338
return RedirectToAction(nameof(ManageLogins), new { Message = message });
342339
}

Sqlite/Sqlite/EfSqlite.csproj

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

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
55
</PropertyGroup>
66

77
<PropertyGroup>
8-
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
8+
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81</AssetTargetFallback>
99
</PropertyGroup>
1010

1111
<PropertyGroup>
1212
<UserSecretsId>aspnet-Sqlite-5f842dd2-e86e-4bd6-9a88-e2d35188f2ae</UserSecretsId>
1313
</PropertyGroup>
1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
16-
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
17-
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
18-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
19-
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
20-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
21-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
22-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
23-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
24-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
25-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.1" />
26-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" PrivateAssets="All" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
28-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
29-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" PrivateAssets="All" />
30-
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
15+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
3116
</ItemGroup>
3217
<ItemGroup>
33-
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.*" />
34-
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.*" />
35-
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.*" />
18+
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.*" />
19+
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.*" />
20+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.*" />
3621
</ItemGroup>
3722

3823
</Project>

Sqlite/Sqlite/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

73
namespace Sqlite.Models
84
{
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Http.Authentication;
1+
using System.Collections.Generic;
2+
using Microsoft.AspNetCore.Authentication;
63
using Microsoft.AspNetCore.Identity;
74

85
namespace Sqlite.Models.ManageViewModels
@@ -11,6 +8,6 @@ public class ManageLoginsViewModel
118
{
129
public IList<UserLoginInfo> CurrentLogins { get; set; }
1310

14-
public IList<AuthenticationDescription> OtherLogins { get; set; }
11+
public IList<AuthenticationScheme> OtherLogins { get; set; }
1512
}
1613
}

Sqlite/Sqlite/Program.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
1+
using Microsoft.AspNetCore;
62
using Microsoft.AspNetCore.Hosting;
73

84
namespace Sqlite
@@ -11,15 +7,12 @@ public class Program
117
{
128
public static void Main(string[] args)
139
{
14-
var host = new WebHostBuilder()
15-
.UseKestrel()
16-
.UseContentRoot(Directory.GetCurrentDirectory())
17-
.UseIISIntegration()
10+
BuildWebHost(args).Run();
11+
}
12+
13+
public static IWebHost BuildWebHost(string[] args) =>
14+
WebHost.CreateDefaultBuilder(args)
1815
.UseStartup<Startup>()
19-
.UseApplicationInsights()
2016
.Build();
21-
22-
host.Run();
23-
}
2417
}
2518
}

Sqlite/Sqlite/Startup.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
62
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
83
using Microsoft.EntityFrameworkCore;
94
using Microsoft.Extensions.Configuration;
105
using Microsoft.Extensions.DependencyInjection;
@@ -13,6 +8,7 @@
138
using Sqlite.Models;
149
using Sqlite.Services;
1510
using EfSqlite.Data;
11+
using Microsoft.AspNetCore.Identity;
1612

1713
namespace Sqlite
1814
{
@@ -77,7 +73,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
7773

7874
app.UseStaticFiles();
7975

80-
app.UseIdentity();
76+
app.UseAuthentication();
8177

8278
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
8379

Sqlite/Sqlite/Views/Account/Login.cshtml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
@using System.Collections.Generic
2-
@using Microsoft.AspNetCore.Http
3-
@using Microsoft.AspNetCore.Http.Authentication
1+
@using System.Linq
42
@model LoginViewModel
53
@inject SignInManager<ApplicationUser> SignInManager
64

@@ -59,7 +57,7 @@
5957
<h4>Use another service to log in.</h4>
6058
<hr />
6159
@{
62-
var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList();
60+
var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
6361
if (loginProviders.Count == 0)
6462
{
6563
<div>
@@ -74,9 +72,9 @@
7472
<form asp-controller="Account" asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
7573
<div>
7674
<p>
77-
@foreach (var provider in loginProviders)
78-
{
79-
<button type="submit" class="btn btn-default" name="provider" value="@provider.AuthenticationScheme" title="Log in using your @provider.DisplayName account">@provider.AuthenticationScheme</button>
75+
@foreach (var provider in loginProviders)
76+
{
77+
<button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.Name</button>
8078
}
8179
</p>
8280
</div>

0 commit comments

Comments
 (0)