-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
595c194
commit 0f94b3d
Showing
12 changed files
with
227 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using EspressoShop.Web.Models; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EspressoShop.Web.Controllers | ||
{ | ||
[Route("[controller]/[action]")] | ||
public class AccountController : Controller | ||
{ | ||
private readonly ILogger<AccountController> _logger; | ||
|
||
public AccountController(ILogger<AccountController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[AllowAnonymous] | ||
public ActionResult Login(string returnUrl) | ||
{ | ||
ViewBag.ReturnUrl = returnUrl; | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
[AllowAnonymous] | ||
[ValidateAntiForgeryToken] | ||
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) | ||
{ | ||
if (!ModelState.IsValid) return View(); | ||
|
||
var claimsIdentity = AuthenticateUser(model.Email, model.Password); | ||
|
||
if (claimsIdentity == null) | ||
{ | ||
ModelState.AddModelError(string.Empty, "Invalid login attempt."); | ||
return View(); | ||
} | ||
|
||
await HttpContext.SignInAsync( | ||
CookieAuthenticationDefaults.AuthenticationScheme, | ||
new ClaimsPrincipal(claimsIdentity), | ||
new AuthenticationProperties()); | ||
|
||
_logger.LogInformation($"User {model.Email} logged in at {DateTime.UtcNow}."); | ||
|
||
return Redirect(returnUrl ?? "/"); | ||
|
||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Logout() | ||
{ | ||
_logger.LogInformation($"User {User.Identity.Name} logged out at {DateTime.UtcNow}."); | ||
|
||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); | ||
|
||
return Redirect("/"); | ||
} | ||
|
||
private ClaimsIdentity AuthenticateUser(string email, string password) | ||
{ | ||
// For demonstration purposes, authenticate a user | ||
// with a static email address. Ignore the password. | ||
|
||
if (email == "[email protected]") | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new Claim( ClaimTypes.Name, "Administrator"), | ||
new Claim(ClaimTypes.Email, email), | ||
new Claim(ClaimTypes.Role, "Administrator") | ||
}; | ||
|
||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); | ||
|
||
return claimsIdentity; | ||
} | ||
if (email == "[email protected]") | ||
{ | ||
var claims = new List<Claim> | ||
{ | ||
new Claim( ClaimTypes.Name, "Hossam Barakat"), | ||
new Claim(ClaimTypes.Email, email), | ||
new Claim(ClaimTypes.Role, "User") | ||
}; | ||
|
||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); | ||
|
||
return claimsIdentity; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace EspressoShop.Web.Models | ||
{ | ||
public class ApplicationUser | ||
{ | ||
public string Email { get; set; } | ||
public string FullName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace EspressoShop.Web.Models | ||
{ | ||
public class LoginViewModel | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
[Display(Name = "Email")] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Password)] | ||
[Display(Name = "Password")] | ||
public string Password { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@model LoginViewModel | ||
|
||
@{ | ||
ViewBag.Title = "Log in"; | ||
} | ||
|
||
<h2>@ViewBag.Title.</h2> | ||
<div class="row"> | ||
<div class="col-md-8"> | ||
<section id="loginForm"> | ||
<form asp-controller="Account" asp-action="Login" class = "form-horizontal"role = "form" method="post"> | ||
@Html.AntiForgeryToken() | ||
<hr/> | ||
<div asp-validation-summary="All" class="text-danger"></div> | ||
<div class="form-group"> | ||
<label asp-for="Email"></label> | ||
<input asp-for="Email" class="form-control"> | ||
<span asp-validation-for="Email" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Password"></label> | ||
<input asp-for="Password" class="form-control"> | ||
<span asp-validation-for="Password" class="text-danger"></span> | ||
</div> | ||
<div class="form-group"> | ||
<button type="submit" class="btn btn-default btn-primary">Log in</button> | ||
</div> | ||
</form> | ||
</section> | ||
</div> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor; | ||
|
||
@if (HttpContextAccessor.HttpContext.User.Identity.IsAuthenticated) | ||
{ | ||
<span class="navbar-text ml-auto text-white">Welcome, @HttpContextAccessor.HttpContext.User.Identity.Name</span> | ||
<ul class="navbar-nav navbar-right"> | ||
|
||
<li class="nav-item"> | ||
<form asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right"> | ||
<button type="submit" class="btn btn-link text-white">Log out</button> | ||
</form> | ||
</li> | ||
</ul> | ||
|
||
} | ||
else | ||
{ | ||
<ul class="nav navbar-nav navbar-right"> | ||
<li><a asp-action="Login" asp-controller="Account" class="text-white">Log in</a></li> | ||
</ul> | ||
} |
Oops, something went wrong.