-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a HttpContextPrincipalResolver and testpages
- Loading branch information
1 parent
a5591f4
commit 3b4d917
Showing
9 changed files
with
248 additions
and
2 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
30 changes: 30 additions & 0 deletions
30
Zetbox.Client.ASPNET.Toolkit/HttpContextPrincipalResolver.cs
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,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
using Autofac; | ||
using Microsoft.AspNetCore.Http; | ||
using Zetbox.API; | ||
using Zetbox.API.Common; | ||
|
||
namespace Zetbox.Client.ASPNET.Toolkit | ||
{ | ||
public class HttpContextPrincipalResolver : BasePrincipalResolver | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public HttpContextPrincipalResolver(ILifetimeScope parentScope, IHttpContextAccessor httpContextAccessor) : base(parentScope) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public override ZetboxPrincipal GetCurrent() | ||
{ | ||
if (!string.IsNullOrEmpty(_httpContextAccessor.HttpContext?.User?.Identity?.Name)) | ||
return Resolve(_httpContextAccessor.HttpContext?.User?.Identity); | ||
else | ||
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,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using log4net; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; | ||
using Zetbox.API; | ||
using Zetbox.API.Server; | ||
using Zetbox.App.Base; | ||
using Zetbox.App.Extensions; | ||
using Zetbox.Client.Presentables; | ||
using DataType = System.ComponentModel.DataAnnotations.DataType; | ||
|
||
namespace Zetbox.Client.ASPNET.Controllers | ||
{ | ||
public class LoginModel | ||
{ | ||
[Required] | ||
[Display(Name = "Benutzername")] | ||
public string UserName { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Password)] | ||
[Display(Name = "Passwort")] | ||
public string Password { get; set; } | ||
|
||
[Display(Name = "Eingeloggt bleiben?")] | ||
public bool RememberMe { get; set; } | ||
} | ||
|
||
[Authorize] | ||
public class AccountController: ZetboxController | ||
{ | ||
private static readonly ILog _log = LogManager.GetLogger(typeof(AccountController)); | ||
|
||
private readonly Func<IZetboxContext> _ctxFactory; | ||
private readonly Func<IZetboxServerContext> _srvCtxFactory; | ||
|
||
public AccountController(IViewModelFactory vmf, ZetboxContextHttpScope contextScope, Func<IZetboxContext> ctxFactory, Func<IZetboxServerContext> srvCtxFactory) | ||
: base(vmf, contextScope) | ||
{ | ||
_ctxFactory = ctxFactory; | ||
_srvCtxFactory = srvCtxFactory; | ||
} | ||
|
||
// | ||
// GET: /Account/Login | ||
|
||
[AllowAnonymous] | ||
public ActionResult Login(string returnUrl) | ||
{ | ||
ViewBag.ReturnUrl = returnUrl; | ||
return View(); | ||
} | ||
|
||
// | ||
// POST: /Account/Login | ||
|
||
[HttpPost] | ||
[AllowAnonymous] | ||
public async Task<ActionResult> Login(LoginModel model, string returnUrl) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
var zbIdentity = DataContext.GetQuery<Identity>().SingleOrDefault(i => i.UserName.ToLower() == model.UserName.ToLower()); | ||
if (zbIdentity == null) | ||
{ | ||
ModelState.AddModelError("", "Kein Eintrag unter diesem Benutzernamen und Passwort gefunden."); | ||
return View(model); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(zbIdentity.Password)) | ||
{ | ||
ModelState.AddModelError("", "Kein Eintrag unter diesem Benutzernamen und Passwort gefunden."); | ||
return View(model); | ||
} | ||
|
||
if (!BCrypt.Net.BCrypt.Verify(model.Password, zbIdentity.Password) == true) | ||
{ | ||
ModelState.AddModelError("", "Kein Eintrag unter diesem Benutzernamen und Passwort gefunden."); | ||
return View(model); | ||
} | ||
|
||
_log.InfoFormat("User {0} logged in", model.UserName); | ||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, model.UserName), | ||
new Claim("FullName", zbIdentity.DisplayName.IfNullOrWhiteSpace(model.UserName)), | ||
}; | ||
|
||
var claimsIdentity = new ClaimsIdentity( | ||
claims, CookieAuthenticationDefaults.AuthenticationScheme); | ||
|
||
var authProperties = new AuthenticationProperties { }; | ||
|
||
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity), authProperties); | ||
return Redirect("/"); | ||
} | ||
|
||
_log.WarnFormat("User {0} failed logging in", model.UserName); | ||
// If we got this far, something failed, redisplay form | ||
ModelState.AddModelError("", "Kein Eintrag unter diesem Benutzernamen und Passwort gefunden."); | ||
return View(model); | ||
} | ||
} | ||
} |
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,46 @@ | ||
@model Zetbox.Client.ASPNET.Controllers.LoginModel | ||
@using Zetbox.Client.ASPNET | ||
|
||
@{ | ||
ViewBag.Title = "Anmelden"; | ||
} | ||
|
||
<h1>@ViewBag.Title</h1> | ||
<div class="row"> | ||
<div class="col-md-8"> | ||
<section id="loginForm"> | ||
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, false, new { @class = "form-horizontal", role = "form" })) | ||
{ | ||
@Html.AntiForgeryToken() | ||
@Html.BootstrapValidationSummary(excludePropertyErrors: true) | ||
<div class="form-group"> | ||
@Html.LabelFor(m => m.UserName, new { @class = "col-md-3 control-label" }) | ||
<div class="col-md-9"> | ||
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control autofocus" }) | ||
@Html.ValidationMessageFor(m => m.UserName) | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
@Html.LabelFor(m => m.Password, new { @class = "col-md-3 control-label" }) | ||
<div class="col-md-9"> | ||
@Html.PasswordFor(m => m.Password, new { @class = "form-control" }) | ||
@Html.ValidationMessageFor(m => m.Password) | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="col-md-offset-3 col-md-9"> | ||
<div class="checkbox"> | ||
@Html.CheckBoxFor(m => m.RememberMe) | ||
@Html.LabelFor(m => m.RememberMe) | ||
</div> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="col-md-offset-3 col-md-9"> | ||
<input type="submit" value="Log in" class="btn btn-primary" rel="nofollow" /> | ||
</div> | ||
</div> | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
@model Zetbox.Client.ASPNET.ViewModels.MvcProjektViewModel | ||
|
||
@{ | ||
ViewData["Title"] = "Edit a Project"; | ||
} | ||
|
||
<h1>@ViewBag.Title</h1> | ||
|
||
@using (Html.BeginForm("Edit", "Projekt", FormMethod.Post, new { @class = "form-horizontal" })) | ||
{ | ||
@Html.AntiForgeryToken() | ||
@Html.BootstrapValidationSummary() | ||
@Html.ZbHiddenID(Model.ID) | ||
@await Html.PartialAsync("_ProjektEditor", Model) | ||
@Html.StatusMessage(Model.StatusMessage) | ||
<div class="form-group"> | ||
<div class="col-md-offset-3 col-md-6"> | ||
<button id="btnSave" type="submit" class="btn btn-primary">Save</button> | ||
<a href="@Url.Action("Index", "Projekt")" class="btn btn-default">Back</a> | ||
</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