Skip to content

Commit

Permalink
Upgrade to ASP.NET Core RC 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Anderson committed May 20, 2016
1 parent e38bded commit 3bea9c6
Show file tree
Hide file tree
Showing 42 changed files with 274 additions and 250 deletions.
2 changes: 1 addition & 1 deletion global.json
@@ -1,6 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-rc1-update1"
"version": "1.0.0-preview1-002702"
}
}
41 changes: 22 additions & 19 deletions src/ASP.NET-Core-SPAs/API/ContactsApiController.cs
@@ -1,13 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ASP.NET_Core_SPAs.Contexts;
using ASP.NET_Core_SPAs.Models;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace ASP.NET_Core_SPAs.API
{
Expand All @@ -17,9 +17,12 @@ namespace ASP.NET_Core_SPAs.API
public class ContactsApiController : Controller
{
private readonly ContactsDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;

public ContactsApiController(ContactsDbContext context)
public ContactsApiController(UserManager<ApplicationUser> userManager,
ContactsDbContext context)
{
_userManager = userManager;
_context = context;
}

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

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

if (contact == null)
{
return HttpNotFound();
return NotFound();
}

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

if (id != contact.Id ||
User.GetUserId() != contact.UserId)
_userManager.GetUserId(User) != contact.UserId)
{
return HttpBadRequest();
return BadRequest();
}

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

throw;
}

return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
return new StatusCodeResult(StatusCodes.Status204NoContent);
}

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

contact.UserId = User.GetUserId();
contact.UserId = _userManager.GetUserId(User);
_context.Contacts.Add(contact);
try
{
Expand All @@ -102,7 +105,7 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
{
if (ContactExists(contact.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
return new StatusCodeResult(StatusCodes.Status409Conflict);
}

throw;
Expand All @@ -117,13 +120,13 @@ public async Task<IActionResult> DeleteContact([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
return BadRequest(ModelState);
}

var contact = await GetContacts().SingleAsync(m => m.Id == id);
if (contact == null)
{
return HttpNotFound();
return NotFound();
}

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

private IQueryable<Contact> GetContacts()
{
return _context.Contacts.Where(c => c.UserId == User.GetUserId());
return _context.Contacts.Where(c => c.UserId == _userManager.GetUserId(User));
}
}
}
9 changes: 7 additions & 2 deletions src/ASP.NET-Core-SPAs/ASP.NET-Core-SPAs.xproj
Expand Up @@ -9,7 +9,7 @@
<ProjectGuid>33cacbf0-4bcc-4af8-ab11-adbcb6259f88</ProjectGuid>
<RootNamespace>ASP.NET_Core_SPAs</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
Expand All @@ -20,4 +20,9 @@
<DnxInvisibleContent Include="package.json" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
<ProjectExtensions>
<VisualStudio>
<UserProperties project_1json__JSONSchema="http://json.schemastore.org/project" />
</VisualStudio>
</ProjectExtensions>
</Project>
11 changes: 9 additions & 2 deletions src/ASP.NET-Core-SPAs/Contexts/ContactsDbContext.cs
@@ -1,5 +1,6 @@
using ASP.NET_Core_SPAs.Models;
using Microsoft.Data.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

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

public ContactsDbContext()
public ContactsDbContext(DbContextOptions<ContactsDbContext> options)
: base(options)
{
if (_created) return;
Database.Migrate();
Expand All @@ -17,6 +19,11 @@ public ContactsDbContext()

protected override void OnModelCreating(ModelBuilder builder)
{
foreach (var entity in builder.Model.GetEntityTypes())
{
entity.Relational().TableName = entity.DisplayName();
}

builder.Entity<Contact>().HasKey(c => c.Id);
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/ASP.NET-Core-SPAs/Controllers/AccountController.cs
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using ASP.NET_Core_SPAs.Models;
using ASP.NET_Core_SPAs.Services;
Expand Down Expand Up @@ -181,7 +178,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
// If the user does not have an account, then ask the user to create an account.
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;
var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
}
}
Expand All @@ -193,7 +190,7 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
[ValidateAntiForgeryToken]
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
{
if (User.IsSignedIn())
if (_signInManager.IsSignedIn(User))
{
return RedirectToAction(nameof(ManageController.Index), "Manage");
}
Expand Down Expand Up @@ -447,7 +444,7 @@ private void AddErrors(IdentityResult result)

private async Task<ApplicationUser> GetCurrentUserAsync()
{
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
return await _userManager.GetUserAsync(HttpContext.User);
}

private IActionResult RedirectToLocal(string returnUrl)
Expand Down
29 changes: 16 additions & 13 deletions src/ASP.NET-Core-SPAs/Controllers/ContactsController.cs
@@ -1,21 +1,24 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.AspNetCore.Mvc;
using ASP.NET_Core_SPAs.Contexts;
using ASP.NET_Core_SPAs.Models;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace ASP.NET_Core_SPAs.Controllers
{
[Authorize]
public class ContactsController : Controller
{
private readonly ContactsDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;

public ContactsController(ContactsDbContext context)
public ContactsController(UserManager<ApplicationUser> userManager,
ContactsDbContext context)
{
_userManager = userManager;
_context = context;
}

Expand All @@ -30,13 +33,13 @@ public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return HttpNotFound();
return NotFound();
}

var contact = await GetContacts().SingleAsync(m => m.Id == id);
if (contact == null)
{
return HttpNotFound();
return NotFound();
}

return View(contact);
Expand All @@ -55,7 +58,7 @@ public async Task<IActionResult> Create(Contact contact)
{
if (ModelState.IsValid)
{
contact.UserId = User.GetUserId();
contact.UserId = _userManager.GetUserId(User);
_context.Contacts.Add(contact);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
Expand All @@ -68,13 +71,13 @@ public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return HttpNotFound();
return NotFound();
}

var contact = await GetContacts().SingleAsync(m => m.Id == id);
if (contact == null)
{
return HttpNotFound();
return NotFound();
}
return View(contact);
}
Expand All @@ -99,13 +102,13 @@ public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return HttpNotFound();
return NotFound();
}

var contact = await GetContacts().SingleAsync(m => m.Id == id);
if (contact == null)
{
return HttpNotFound();
return NotFound();
}

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

private IQueryable<Contact> GetContacts()
{
return _context.Contacts.Where(c => c.UserId == User.GetUserId());
return _context.Contacts.Where(c => c.UserId == _userManager.GetUserId(User));
}
}
}
6 changes: 1 addition & 5 deletions src/ASP.NET-Core-SPAs/Controllers/HomeController.cs
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace ASP.NET_Core_SPAs.Controllers
{
Expand Down
19 changes: 8 additions & 11 deletions src/ASP.NET-Core-SPAs/Controllers/ManageController.cs
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ASP.NET_Core_SPAs.Models;
using ASP.NET_Core_SPAs.Services;
Expand Down Expand Up @@ -291,7 +288,7 @@ public IActionResult LinkLogin(string provider)
{
// Request a redirect to the external login provider to link a login for the current user
var redirectUrl = Url.Action("LinkLoginCallback", "Manage");
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.GetUserId());
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User));
return new ChallengeResult(provider, properties);
}

Expand All @@ -305,7 +302,7 @@ public async Task<ActionResult> LinkLoginCallback()
{
return View("Error");
}
var info = await _signInManager.GetExternalLoginInfoAsync(User.GetUserId());
var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user));
if (info == null)
{
return RedirectToAction(nameof(ManageLogins), new { Message = ManageMessageId.Error });
Expand Down Expand Up @@ -337,9 +334,9 @@ public enum ManageMessageId
Error
}

private async Task<ApplicationUser> GetCurrentUserAsync()
private Task<ApplicationUser> GetCurrentUserAsync()
{
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
return _userManager.GetUserAsync(HttpContext.User);
}

#endregion
Expand Down

0 comments on commit 3bea9c6

Please sign in to comment.