Skip to content

Commit 3bea9c6

Browse files
author
Eric Anderson
committed
Upgrade to ASP.NET Core RC 2
1 parent e38bded commit 3bea9c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+274
-250
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"projects": [ "src", "test" ],
33
"sdk": {
4-
"version": "1.0.0-rc1-update1"
4+
"version": "1.0.0-preview1-002702"
55
}
66
}

src/ASP.NET-Core-SPAs/API/ContactsApiController.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using System.Security.Claims;
43
using System.Threading.Tasks;
5-
using Microsoft.AspNet.Http;
6-
using Microsoft.AspNet.Mvc;
7-
using Microsoft.Data.Entity;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.EntityFrameworkCore;
87
using ASP.NET_Core_SPAs.Contexts;
98
using ASP.NET_Core_SPAs.Models;
10-
using Microsoft.AspNet.Authorization;
9+
using Microsoft.AspNetCore.Authorization;
10+
using Microsoft.AspNetCore.Identity;
1111

1212
namespace ASP.NET_Core_SPAs.API
1313
{
@@ -17,9 +17,12 @@ namespace ASP.NET_Core_SPAs.API
1717
public class ContactsApiController : Controller
1818
{
1919
private readonly ContactsDbContext _context;
20+
private readonly UserManager<ApplicationUser> _userManager;
2021

21-
public ContactsApiController(ContactsDbContext context)
22+
public ContactsApiController(UserManager<ApplicationUser> userManager,
23+
ContactsDbContext context)
2224
{
25+
_userManager = userManager;
2326
_context = context;
2427
}
2528

@@ -36,14 +39,14 @@ public async Task<IActionResult> GetContact([FromRoute] int id)
3639
{
3740
if (!ModelState.IsValid)
3841
{
39-
return HttpBadRequest(ModelState);
42+
return BadRequest(ModelState);
4043
}
4144

4245
var contact = await GetContacts().SingleAsync(m => m.Id == id);
4346

4447
if (contact == null)
4548
{
46-
return HttpNotFound();
49+
return NotFound();
4750
}
4851

4952
return Ok(contact);
@@ -55,13 +58,13 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
5558
{
5659
if (!ModelState.IsValid)
5760
{
58-
return HttpBadRequest(ModelState);
61+
return BadRequest(ModelState);
5962
}
6063

6164
if (id != contact.Id ||
62-
User.GetUserId() != contact.UserId)
65+
_userManager.GetUserId(User) != contact.UserId)
6366
{
64-
return HttpBadRequest();
67+
return BadRequest();
6568
}
6669

6770
_context.Entry(contact).State = EntityState.Modified;
@@ -74,13 +77,13 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
7477
{
7578
if (!ContactExists(id))
7679
{
77-
return HttpNotFound();
80+
return NotFound();
7881
}
7982

8083
throw;
8184
}
8285

83-
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
86+
return new StatusCodeResult(StatusCodes.Status204NoContent);
8487
}
8588

8689
// POST: api/ContactsApi
@@ -89,10 +92,10 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
8992
{
9093
if (!ModelState.IsValid)
9194
{
92-
return HttpBadRequest(ModelState);
95+
return BadRequest(ModelState);
9396
}
9497

95-
contact.UserId = User.GetUserId();
98+
contact.UserId = _userManager.GetUserId(User);
9699
_context.Contacts.Add(contact);
97100
try
98101
{
@@ -102,7 +105,7 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
102105
{
103106
if (ContactExists(contact.Id))
104107
{
105-
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
108+
return new StatusCodeResult(StatusCodes.Status409Conflict);
106109
}
107110

108111
throw;
@@ -117,13 +120,13 @@ public async Task<IActionResult> DeleteContact([FromRoute] int id)
117120
{
118121
if (!ModelState.IsValid)
119122
{
120-
return HttpBadRequest(ModelState);
123+
return BadRequest(ModelState);
121124
}
122125

123126
var contact = await GetContacts().SingleAsync(m => m.Id == id);
124127
if (contact == null)
125128
{
126-
return HttpNotFound();
129+
return NotFound();
127130
}
128131

129132
_context.Contacts.Remove(contact);
@@ -148,7 +151,7 @@ private bool ContactExists(int id)
148151

149152
private IQueryable<Contact> GetContacts()
150153
{
151-
return _context.Contacts.Where(c => c.UserId == User.GetUserId());
154+
return _context.Contacts.Where(c => c.UserId == _userManager.GetUserId(User));
152155
}
153156
}
154157
}

src/ASP.NET-Core-SPAs/ASP.NET-Core-SPAs.xproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ProjectGuid>33cacbf0-4bcc-4af8-ab11-adbcb6259f88</ProjectGuid>
1010
<RootNamespace>ASP.NET_Core_SPAs</RootNamespace>
1111
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12-
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1313
</PropertyGroup>
1414
<PropertyGroup>
1515
<SchemaVersion>2.0</SchemaVersion>
@@ -20,4 +20,9 @@
2020
<DnxInvisibleContent Include="package.json" />
2121
</ItemGroup>
2222
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
23-
</Project>
23+
<ProjectExtensions>
24+
<VisualStudio>
25+
<UserProperties project_1json__JSONSchema="http://json.schemastore.org/project" />
26+
</VisualStudio>
27+
</ProjectExtensions>
28+
</Project>

src/ASP.NET-Core-SPAs/Contexts/ContactsDbContext.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ASP.NET_Core_SPAs.Models;
2-
using Microsoft.Data.Entity;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
34

45
namespace ASP.NET_Core_SPAs.Contexts
56
{
@@ -8,7 +9,8 @@ public sealed class ContactsDbContext : DbContext
89
private static bool _created;
910
public DbSet<Contact> Contacts { get; set; }
1011

11-
public ContactsDbContext()
12+
public ContactsDbContext(DbContextOptions<ContactsDbContext> options)
13+
: base(options)
1214
{
1315
if (_created) return;
1416
Database.Migrate();
@@ -17,6 +19,11 @@ public ContactsDbContext()
1719

1820
protected override void OnModelCreating(ModelBuilder builder)
1921
{
22+
foreach (var entity in builder.Model.GetEntityTypes())
23+
{
24+
entity.Relational().TableName = entity.DisplayName();
25+
}
26+
2027
builder.Entity<Contact>().HasKey(c => c.Id);
2128
}
2229
}

src/ASP.NET-Core-SPAs/Controllers/AccountController.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Linq;
42
using System.Security.Claims;
53
using System.Threading.Tasks;
6-
using Microsoft.AspNet.Authorization;
7-
using Microsoft.AspNet.Identity;
8-
using Microsoft.AspNet.Mvc;
9-
using Microsoft.AspNet.Mvc.Rendering;
10-
using Microsoft.Data.Entity;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Identity;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.Rendering;
118
using Microsoft.Extensions.Logging;
129
using ASP.NET_Core_SPAs.Models;
1310
using ASP.NET_Core_SPAs.Services;
@@ -181,7 +178,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
181178
// If the user does not have an account, then ask the user to create an account.
182179
ViewData["ReturnUrl"] = returnUrl;
183180
ViewData["LoginProvider"] = info.LoginProvider;
184-
var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);
181+
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
185182
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
186183
}
187184
}
@@ -193,7 +190,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
193190
[ValidateAntiForgeryToken]
194191
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
195192
{
196-
if (User.IsSignedIn())
193+
if (_signInManager.IsSignedIn(User))
197194
{
198195
return RedirectToAction(nameof(ManageController.Index), "Manage");
199196
}
@@ -447,7 +444,7 @@ private void AddErrors(IdentityResult result)
447444

448445
private async Task<ApplicationUser> GetCurrentUserAsync()
449446
{
450-
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
447+
return await _userManager.GetUserAsync(HttpContext.User);
451448
}
452449

453450
private IActionResult RedirectToLocal(string returnUrl)

src/ASP.NET-Core-SPAs/Controllers/ContactsController.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
using System.Linq;
2-
using System.Security.Claims;
32
using System.Threading.Tasks;
4-
using Microsoft.AspNet.Mvc;
5-
using Microsoft.Data.Entity;
3+
using Microsoft.AspNetCore.Mvc;
64
using ASP.NET_Core_SPAs.Contexts;
75
using ASP.NET_Core_SPAs.Models;
8-
using Microsoft.AspNet.Authorization;
6+
using Microsoft.AspNetCore.Authorization;
7+
using Microsoft.AspNetCore.Identity;
8+
using Microsoft.EntityFrameworkCore;
99

1010
namespace ASP.NET_Core_SPAs.Controllers
1111
{
1212
[Authorize]
1313
public class ContactsController : Controller
1414
{
1515
private readonly ContactsDbContext _context;
16+
private readonly UserManager<ApplicationUser> _userManager;
1617

17-
public ContactsController(ContactsDbContext context)
18+
public ContactsController(UserManager<ApplicationUser> userManager,
19+
ContactsDbContext context)
1820
{
21+
_userManager = userManager;
1922
_context = context;
2023
}
2124

@@ -30,13 +33,13 @@ public async Task<IActionResult> Details(int? id)
3033
{
3134
if (id == null)
3235
{
33-
return HttpNotFound();
36+
return NotFound();
3437
}
3538

3639
var contact = await GetContacts().SingleAsync(m => m.Id == id);
3740
if (contact == null)
3841
{
39-
return HttpNotFound();
42+
return NotFound();
4043
}
4144

4245
return View(contact);
@@ -55,7 +58,7 @@ public async Task<IActionResult> Create(Contact contact)
5558
{
5659
if (ModelState.IsValid)
5760
{
58-
contact.UserId = User.GetUserId();
61+
contact.UserId = _userManager.GetUserId(User);
5962
_context.Contacts.Add(contact);
6063
await _context.SaveChangesAsync();
6164
return RedirectToAction("Index");
@@ -68,13 +71,13 @@ public async Task<IActionResult> Edit(int? id)
6871
{
6972
if (id == null)
7073
{
71-
return HttpNotFound();
74+
return NotFound();
7275
}
7376

7477
var contact = await GetContacts().SingleAsync(m => m.Id == id);
7578
if (contact == null)
7679
{
77-
return HttpNotFound();
80+
return NotFound();
7881
}
7982
return View(contact);
8083
}
@@ -99,13 +102,13 @@ public async Task<IActionResult> Delete(int? id)
99102
{
100103
if (id == null)
101104
{
102-
return HttpNotFound();
105+
return NotFound();
103106
}
104107

105108
var contact = await GetContacts().SingleAsync(m => m.Id == id);
106109
if (contact == null)
107110
{
108-
return HttpNotFound();
111+
return NotFound();
109112
}
110113

111114
return View(contact);
@@ -124,7 +127,7 @@ public async Task<IActionResult> DeleteConfirmed(int id)
124127

125128
private IQueryable<Contact> GetContacts()
126129
{
127-
return _context.Contacts.Where(c => c.UserId == User.GetUserId());
130+
return _context.Contacts.Where(c => c.UserId == _userManager.GetUserId(User));
128131
}
129132
}
130133
}

src/ASP.NET-Core-SPAs/Controllers/HomeController.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.AspNet.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
62

73
namespace ASP.NET_Core_SPAs.Controllers
84
{

src/ASP.NET-Core-SPAs/Controllers/ManageController.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Linq;
42
using System.Threading.Tasks;
5-
using System.Security.Claims;
6-
using Microsoft.AspNet.Authorization;
7-
using Microsoft.AspNet.Identity;
8-
using Microsoft.AspNet.Mvc;
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Identity;
5+
using Microsoft.AspNetCore.Mvc;
96
using Microsoft.Extensions.Logging;
107
using ASP.NET_Core_SPAs.Models;
118
using ASP.NET_Core_SPAs.Services;
@@ -291,7 +288,7 @@ public IActionResult LinkLogin(string provider)
291288
{
292289
// Request a redirect to the external login provider to link a login for the current user
293290
var redirectUrl = Url.Action("LinkLoginCallback", "Manage");
294-
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.GetUserId());
291+
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User));
295292
return new ChallengeResult(provider, properties);
296293
}
297294

@@ -305,7 +302,7 @@ public async Task<ActionResult> LinkLoginCallback()
305302
{
306303
return View("Error");
307304
}
308-
var info = await _signInManager.GetExternalLoginInfoAsync(User.GetUserId());
305+
var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user));
309306
if (info == null)
310307
{
311308
return RedirectToAction(nameof(ManageLogins), new { Message = ManageMessageId.Error });
@@ -337,9 +334,9 @@ public enum ManageMessageId
337334
Error
338335
}
339336

340-
private async Task<ApplicationUser> GetCurrentUserAsync()
337+
private Task<ApplicationUser> GetCurrentUserAsync()
341338
{
342-
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
339+
return _userManager.GetUserAsync(HttpContext.User);
343340
}
344341

345342
#endregion

0 commit comments

Comments
 (0)