Added custom redirection url after authorization
This commit is contained in:
@@ -10,6 +10,7 @@ using MyWebsite.Models;
|
|||||||
using MyWebsite.Models.Databases;
|
using MyWebsite.Models.Databases;
|
||||||
using MyWebsite.ViewModels;
|
using MyWebsite.ViewModels;
|
||||||
|
|
||||||
|
#pragma warning disable CA1054 // Uri parameters should not be strings
|
||||||
namespace MyWebsite.Controllers
|
namespace MyWebsite.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
@@ -22,15 +23,15 @@ namespace MyWebsite.Controllers
|
|||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult Login() =>
|
public IActionResult Login(string ReturnUrl) =>
|
||||||
View(new CredentialViewModel(Database));
|
View(new CredentialViewModel(Database, ReturnUrl));
|
||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public async Task<IActionResult> Login(CredentialViewModel model)
|
public async Task<IActionResult> Login(CredentialViewModel model)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid || model == null)
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("Authorization error", "Invalid data");
|
ModelState.AddModelError("Authorization error", "Invalid data");
|
||||||
return View(new CredentialViewModel(Database, model));
|
return View(new CredentialViewModel(Database, model));
|
||||||
@@ -52,7 +53,7 @@ namespace MyWebsite.Controllers
|
|||||||
ClaimsIdentity id = new ClaimsIdentity(new Claim[] { claim }, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
|
ClaimsIdentity id = new ClaimsIdentity(new Claim[] { claim }, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
|
||||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)).ConfigureAwait(false);
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id)).ConfigureAwait(false);
|
||||||
|
|
||||||
return RedirectToAction("Index", "Admin");
|
return Redirect(model.ReturnUrl ?? "/Admin");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Logout()
|
public async Task<IActionResult> Logout()
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
using MyWebsite.Models;
|
using MyWebsite.Models;
|
||||||
using MyWebsite.Models.Databases;
|
using MyWebsite.Models.Databases;
|
||||||
|
|
||||||
|
#pragma warning disable CA1054 // Uri parameters should not be strings
|
||||||
|
#pragma warning disable CA1056 // Uri properties should not be strings
|
||||||
namespace MyWebsite.ViewModels
|
namespace MyWebsite.ViewModels
|
||||||
{
|
{
|
||||||
public class CredentialViewModel : ViewModelBase
|
public class CredentialViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
public CredentialModel Credential { get; set; }
|
public CredentialModel Credential { get; set; }
|
||||||
|
public string ReturnUrl { get; set; }
|
||||||
public CredentialViewModel(DatabaseContext context) : base(context) { }
|
public CredentialViewModel(DatabaseContext context) : base(context) { }
|
||||||
|
|
||||||
|
public CredentialViewModel(DatabaseContext context, string returnUrl) : base(context) =>
|
||||||
|
ReturnUrl = returnUrl;
|
||||||
|
|
||||||
public CredentialViewModel() : base(null) { }
|
public CredentialViewModel() : base(null) { }
|
||||||
public CredentialViewModel(DatabaseContext context, CredentialViewModel model) : base(context) =>
|
public CredentialViewModel(DatabaseContext context, CredentialViewModel model) : base(context) =>
|
||||||
Credential = model?.Credential;
|
Credential = model?.Credential;
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
<article>
|
<article>
|
||||||
<form asp-action="Login" asp-antiforgery="true">
|
<form asp-action="Login" asp-antiforgery="true">
|
||||||
<div asp-validation-summary="All" class="validation-error"></div>
|
<div asp-validation-summary="All" class="validation-error"></div>
|
||||||
|
<input hidden asp-for="ReturnUrl" />
|
||||||
<div>
|
<div>
|
||||||
<label asp-for="Credential.Email">E-mail</label>
|
<label asp-for="Credential.Email">E-mail</label>
|
||||||
<input type="email" asp-for="Credential.Email" />
|
<input type="email" asp-for="Credential.Email" />
|
||||||
@@ -20,7 +21,8 @@
|
|||||||
<input type="password" asp-for="Credential.Password" />
|
<input type="password" asp-for="Credential.Password" />
|
||||||
<span asp-validation-for="Credential.Password"></span>
|
<span asp-validation-for="Credential.Password"></span>
|
||||||
</div>
|
</div>
|
||||||
<input class="btn" style="margin-top:10px" type="submit" value="Login" />
|
|
||||||
|
<input type="submit" value="Login" />
|
||||||
</form>
|
</form>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user