Skip to content

Commit

Permalink
Merge branch 'master' into ardalis/basket
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Apr 21, 2017
2 parents e4b7021 + 408c5d9 commit 222319f
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# eShopOnWeb

Sample ASP.NET Core reference application, powered by Microsoft, demonstrating a monolithic application architecture and deployment model. This reference application is meant to support the <a href='https://aka.ms/webappebook'>[Architecting and Developing Modern Web Applications with ASP.NET Core and Azure eBook]</a>
Sample ASP.NET Core reference application, powered by Microsoft, demonstrating a monolithic application architecture and deployment model. This reference application is meant to support the [Architecting and Developing Modern Web Applications with ASP.NET Core and Azure eBook](https://aka.ms/webappebook)

The **eShopOnWeb** is related to the <a href='https://github.com/dotnet/eShopOnContainers'>eShopOnContainers</a> sample application which, in that case, focuses on a microservices/containers based application architecture. However, **eShopOnWeb** is much simpler in regards its current functionality and focuses on traditional Web Application Development with a single monolithic deployment, no microservices/containers related.
The **eShopOnWeb** is related to the [eShopOnContainers](https://github.com/dotnet/eShopOnContainers) sample application which, in that case, focuses on a microservices/containers based application architecture. However, **eShopOnWeb** is much simpler in regards its current functionality and focuses on traditional Web Application Development with a single monolithic deployment, no microservices/containers related.

> ### DISCLAIMER
> **IMPORTANT:** The current state of this sample application is **ALPHA**, consider it a 0.1 foundational version. Therefore areas will change significantly while refactoring current code and implementing new features. **Feedback with improvements and pull requests from the community are highly appreciated and accepted.**
Expand Down
27 changes: 27 additions & 0 deletions src/Infrastructure/Identity/AppIdentityDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;


namespace Infrastructure.Identity
{
public class AppIdentityDbContext : IdentityDbContext<ApplicationUser>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}

public class ApplicationUser : IdentityUser
{
}

}
11 changes: 11 additions & 0 deletions src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,16 @@
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.0.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.0.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version="1.3.0" />
</ItemGroup>

</Project>
98 changes: 98 additions & 0 deletions src/Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.eShopWeb.Services;
using Microsoft.eShopWeb.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Infrastructure.Identity;

namespace Microsoft.eShopWeb.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly string _externalCookieScheme;


public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IOptions<IdentityCookieOptions> identityCookieOptions

)
{
_userManager = userManager;
_signInManager = signInManager;
_externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;

}

//
// GET: /Account/SignIn
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> SignIn(string returnUrl = null)
{
// Clear the existing external cookie to ensure a clean login process
await HttpContext.Authentication.SignOutAsync(_externalCookieScheme);

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

//
// POST: /Account/SignIn
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SignIn(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
//_logger.LogInformation(1, "User logged in.");
return RedirectToLocal(returnUrl);
}
//if (result.RequiresTwoFactor)
//{
// return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
//}
if (result.IsLockedOut)
{
//_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}

// If we got this far, something failed, redisplay form
return View(model);
}

private IActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction(nameof(CatalogController.Index), "Home");
}
}


}
}
2 changes: 1 addition & 1 deletion src/Web/Infrastructure/CatalogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.eShopWeb.Infrastructure

public class CatalogContext : DbContext
{
public CatalogContext(DbContextOptions options) : base(options)
public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
{
}
public DbSet<CatalogItem> CatalogItems { get; set; }
Expand Down
15 changes: 14 additions & 1 deletion src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Infrastructure.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace Microsoft.eShopWeb
{
Expand All @@ -33,7 +35,7 @@ public void ConfigureServices(IServiceCollection services)
{
try
{
c.UseSqlServer(Configuration["ConnectionString"]);
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
c.ConfigureWarnings(wb =>
{
//By default, in this application, we don't want to have client evaluations
Expand All @@ -46,6 +48,15 @@ public void ConfigureServices(IServiceCollection services)
}
});

// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();


services.AddTransient<ICatalogService, CatalogService>();
services.Configure<CatalogSettings>(Configuration);
services.AddMvc();
Expand All @@ -69,6 +80,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

app.UseStaticFiles();

app.UseIdentity();

app.UseMvc(routes =>
{
routes.MapRoute(
Expand Down
22 changes: 22 additions & 0 deletions src/Web/ViewModels/LoginViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Microsoft.eShopWeb.ViewModels
{

public class LoginViewModel
{
[Required]
[EmailAddress]
public string Email { get; set; }

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}

}
62 changes: 62 additions & 0 deletions src/Web/Views/Account/Signin.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@using System.Collections.Generic
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication
@model LoginViewModel
@{
ViewData["Title"] = "Log in";
}
<div class="brand-header-block">
<ul class="container">
<li><a asp-area="" asp-controller="Account" asp-action="Register">REGISTER</a></li>
<li class="active" style="margin-right: 65px;">LOGIN</li>
</ul>
</div>
<div class="container account-login-container">
<div class="row">
<div class="col-md-12">
<section>
<form asp-controller="Account" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<h4>ARE YOU REGISTERED?</h4>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="control-label form-label"></label>
<input asp-for="Email" class="form-control form-input form-input-center" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label form-label"></label>
<input asp-for="Password" class="form-control form-input form-input-center" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label asp-for="RememberMe">
<input asp-for="RememberMe" />
@Html.DisplayNameFor(m => m.RememberMe)
</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default btn-brand btn-brand-big">&nbsp;LOG IN&nbsp;</button>
</div>
<p>
<a asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" class="text">Register as a new user?</a>
</p>
<p>
Note that for demo purposes you don't need to register and can login with these credentials:
</p>
<p>
User: <b>demouser@microsoft.com</b>
</p>
<p>
Password: <b>Pass@word1</b>
</p>
</form>
</section>
</div>
</div>
</div>

@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
3 changes: 2 additions & 1 deletion src/Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
<img src="../images/brand.png" />
</a>
</section>

@await Html.PartialAsync("_LoginPartial")

</article>
</div>
</header>
Expand Down
57 changes: 57 additions & 0 deletions src/Web/Views/Shared/_LoginPartial.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@using Microsoft.AspNetCore.Identity
@*@inject IIdentityParser<ApplicationUser> UserManager*@

@if (Context.User.Identity.IsAuthenticated)
{
<section class="col-lg-4 col-md-5 col-xs-12">
<div class="esh-identity">
<form asp-area="" asp-controller="Account" asp-action="SignOut" method="post" id="logoutForm" class="navbar-right">
<section class="esh-identity-section">

@*<div class="esh-identity-name">@User.FindFirst(x => x.Type == "preferred_username").Value</div>*@
<img class="esh-identity-image" src="~/images/arrow-down.png">
</section>

<section class="esh-identity-drop">

<a class="esh-identity-item"
asp-controller="Order"
asp-action="Index">

<div class="esh-identity-name esh-identity-name--upper">My orders</div>
<img class="esh-identity-image" src="~/images/my_orders.png">
</a>

<a class="esh-identity-item"
href="javascript:document.getElementById('logoutForm').submit()">

<div class="esh-identity-name esh-identity-name--upper">Log Out</div>
<img class="esh-identity-image" src="~/images/logout.png">
</a>
</section>
</form>
</div>
</section>

<section class="col-lg-1 col-xs-12">
@*@await Component.InvokeAsync("Cart", new { user = UserManager.Parse(User) })*@
</section>

}
else
{
<section class="col-lg-4 col-md-5 col-xs-12">
<div class="esh-identity">
<section class="esh-identity-section">
<div class="esh-identity-item">

<a asp-area="" asp-controller="Account" asp-action="SignIn" class="esh-identity-name esh-identity-name--upper">
Login
</a>
</div>
</section>
</div>
</section>

<section class="col-lg-1 col-xs-12"></section>
}
1 change: 1 addition & 0 deletions src/Web/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@using Microsoft.eShopWeb
@using Microsoft.eShopWeb.ViewModels
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2 changes: 2 additions & 0 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
<ItemGroup>
<Folder Include="Pics\" />
<Folder Include="Views\Catalog\" />
<Folder Include="Views\Account\" />
<Folder Include="wwwroot\css\catalog\" />
<Folder Include="wwwroot\fonts\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion src/Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"ConnectionString": "Server=(localdb)\\ProjectsV13;Integrated Security=true;Initial Catalog=Microsoft.eShopOnContainers.Services.CatalogDb;",
"ConnectionStrings": {
"CatalogConnection": "Server=(localdb)\\ProjectsV13;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.CatalogDb;",
"IdentityConnection": "Server=(localdb)\\ProjectsV13;Integrated Security=true;Initial Catalog=Microsoft.eShopOnWeb.Identity;"
},
"CatalogBaseUrl": "http://localhost:5106",
"Logging": {
"IncludeScopes": false,
Expand Down

0 comments on commit 222319f

Please sign in to comment.